Mandrill Library

The Mandrill Library works the same as the Email Library except it allows you to use your own Mandrill account so you're not subject to the 20 emails/hour limitation we enforce on the Email library. The only difference is in the send function described below, where you will specify your API key.

 

Be sure to specify a sender email that is already registered with your Mandrill account. If you leave the sender as *@mediumone.com it will be rejected by your Mandrill.

 

Import

To use this library and its functions, you must use the import line at the beginning of your Base Python code.

import Mandrill

 

Email Class

Mandrill.Email(sender=None, display_name='', recipients=None, subject='', message='', attachments=None)

The Email class represents an email object.

Credit cost: 0

Parameters

  • senderstr email address of the sender. Domain must be registered with your Mandrill account.
  • display_namestr name of the sender to be displayed by the mail client
  • recipientsstr or [str] the recipient's email address. It also supports multiple email addresses separated by a semi-colon ';' or an array of strings.
  • subjectstr the email subject line
  • messagestr message (will default to text email if set).
  • attachmentsMandrill.EmailAttachment object

Return Value

Mandrill.Email

Exceptions

None

 

Attachment Class

Mandrill.EmailAttachment(datacontent_typename)

The EmailAttachment class represents an email attachment object.

Credit cost: 0

Parameters

  • datastr base64 encoded string of the data to attach
  • content_typestr MIME Content-Type of the data being attached (for example 'image/jpeg;')
  • namestr a name to associate to the attachment

Return Value

Mandrill.EmailAttachment

Exceptions

None

Set email text content

Mandrill.Email.text_message(message)

Set the email message content to send as text. This overrides any message set during Email object creation.

Credit cost: 0

Parameters

  • messagestr the message string.

Return Value

None

Example

import Mandrill
email = Mandrill.Email('no-reply@mydomain.com','my name','you@mail.com','message subject')
email.text_message('from me to you... in text')
email.send(YOUR_MANDRILL_USERNAME,YOUR_MANDRILL_API_KEY)

Exceptions

None

Set email HTML content

Mandrill.Email.html_message(message)

Set the email message content to send as HTML. This overrides any message set during Email object creation.

Credit cost: 0

Parameters

  • messagestr the HTML formatted message string.

Return Value

None

Example

import Mandrill
email = Mandrill.Email('no-reply@mydomain.com','my name','you@mail.com','message subject')
email.html_message('<!DOCTYPE html><html><title>Title</title><head></head><body><h2>From me to you,</h2><p> in HTML</p></body></html>')
email.send(YOUR_MANDRILL_USERNAME,YOUR_MANDRILL_API_KEY)

Exceptions

None

Set sender email

Mandrill.Email.sender(sender)

Set the email of the sender. This overrides any sender's email set during Email object creation.

Credit cost: 0

Parameters

  • senderstr the sender's email address.

Return Value

None

Example

import Mandrill
email = Mandrill.Email('no-reply@mydomain.com','my name','you@mail.com','message subject')
email.sender('me2@mail.com')
email.html_message('<!DOCTYPE html><html><title>Title</title><head></head><body><h2>From me to you,</h2><p> in HTML</p></body></html>')
email.send(YOUR_MANDRILL_USERNAME,YOUR_MANDRILL_API_KEY)

Exceptions

None

Set sender name

Mandrill.Email.display_name(display_name)

Set the name of the sender. This overrides any sender's name set during Email object creation.

Credit cost: 0

Parameters

  • display_namestr the sender's display name.

Return Value

None

Example

import Mandrill
email = Mandrill.Email('no-reply@mydomain.com,'my name','you@mail.com','message subject')
email.display_name('my name too')
email.html_message('<!DOCTYPE html><html><title>Title</title><head></head><body><h2>From me to you,</h2><p> in HTML</p></body></html>')
email.send(YOUR_MANDRILL_USERNAME,YOUR_MANDRILL_API_KEY)

Exceptions

None

Add recipient

Mandrill.Email.add_recipient(recipient)

Add a recipient's email to the list of recipients. This adds to the recipient's email list set during Email object creation.

Credit cost: 0

Parameters

  • recipientstr a recipient's email address.

Return Value

None

Example

import Mandrill
email = Mandrill.Email('no-reply@mydomain.com','my name','you@mail.com','message subject')
email.add_recipient('you2@mail.com')
email.html_message('<!DOCTYPE html><html><title>Title</title><head></head><body><h2>From me to you,</h2><p> in HTML</p></body></html>')
email.send(YOUR_MANDRILL_USERNAME,YOUR_MANDRILL_API_KEY)email.send()

Exceptions

None

Add attachment

Mandrill.Email.add_attachment(attachment)

Add an attachment to the email. This adds to the attachment list.

Credit cost: 0

Parameters

  • attachmentEmailAttachment object.

Return Value

None

Example

import Mandrill
email = Mandrill.Email('no-reply@mydomain.com','my name','you@mail.com','message subject')
attachment = Mandrill.EmailAttachment('iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==', 'image/png;', 'image of a red dot')
email.html_message('<!DOCTYPE html><html><title>Title</title><head></head><body><h2>From me to you,</h2><p> in HTML</p></body></html>')
email.add_attachment(attachment)
email.send(YOUR_MANDRILL_USERNAME,YOUR_MANDRILL_API_KEY)

Exceptions

None

Send email

Mandrill.Email.send(username, apikey)

Send the email message.

Credit cost: 1

Parameters

  • username: str Mandrill username.
  • apikey: str Mandrill API key.

Return Value

None

Exceptions

  • EmailAPIException
    • Invalid email address
  • Exception
    • Email service down
    • Invalid credentials
  • AssertionError
    • No sender specified
    • No subject specified
    • No message specified
    • No recipient specified