Send email via Mailgun

The f451 Communications module allows you to send emails via the Mailgun service using a unified interface. In it’s simplest form, you can send a plain text email to one or more recipients. But you can also send complex individualized HTML-emails with attachments and inline images.

In order to use this service, you’ll need to set up an account with Mailgun. Once that is done, you get your API key and other relevant account credentials.

Note

Please refer to the “Configuration files” section for more detailed information on configuration requirements for the Mailgun email service.

Core features

  • Send plain text and/or HTML emailsAn email can be either plain text only, or have both a plain text version of the message and an HTML version included as well.

  • Supports file attachments and inline imagesEmails can include both file attachments and inline images. Please note that emails cannot exceed 25MB in size (incl. any attachments).

  • Supports multiple ‘to’ addressesEvery email can have up to max 1,000 recipients.

  • Supports optional multiple ‘cc’ and ‘bcc’ addressesEmails support both ‘cc’ and ‘bcc’ recipients. But each recipient in either list counts towards to total max of 1,000 recipients.

Examples

Note

These examples use the send_message_via_email() method. But you can also use the generic send_message() method with the same arguments.

Example – send simple email

 1 comms = Comms(secrets, config)              # Initialize ``Comms`` with credentials
 2                                             # and optional default settings
 3 msg = "Hello world!"
 4 attribs = {
 5     "channels": "f451_mailgun",
 6     "to_email": "batman@example.com",
 7     "subject": "Hello!",
 8 }
 9
10 comms.send_message_via_email(msg, attribs)  # Send email

Example – send email with attachments

 1 comms = Comms(secrets, config)              # Initialize ``Comms`` with credentials
 2                                             # and optional default settings
 3 msg = "Hello world!"
 4 attribs = {
 5     "channels": "f451_mailgun",
 6     "to_email": "batman@example.com",
 7     "subject": "Hello!",
 8     "attachments": ["path/to/joker.txt", "path/to/riddler.txt"],
 9 }
10
11 comms.send_message_via_email(msg, attribs)  # Send email

Example – send email with HTML and inline images

 1 comms = Comms(secrets, config)              # Initialize ``Comms`` with credentials
 2                                             # and optional default settings
 3 msg = "Hello world!"
 4 attribs = {
 5     "channels": "f451_mailgun",
 6     "to_email": "batman@example.com",
 7     "subject": "Hello!",
 8     "html": "<html>Hello world! Look at this image <img src="cid:joker.jpg"></html>",
 9     "inline": "path/to/joker.jpg",
10 }
11
12 comms.send_message_via_email(msg, attribs)  # Send email

Example – send email using recipients data

 1 comms = Comms(secrets, config)              # Initialize ``Comms`` with credentials
 2                                             # and optional default settings
 3 msg = "Hello world!"
 4 attribs = {
 5     "channels": "f451_mailgun",
 6     "to_email": ["batman@example.com", "robin@example.com"],
 7     "subject": "Hello %recipient.name%!",
 8     "html": "<html>Your lucky number is %recipient.lucky%</html>",
 9     "recipient_data": {
10         "batman@example.com": {"name":"Batman", "lucky": 13},
11         "robin@example.com": {"name":"Robin", "lucky": 29}
12     }
13 }
14
15 comms.send_message_via_email(msg, attribs)  # Send email

Additional references