Introduction to Mailgun Email Automation

Mailgun is an email automation service provided by Rackspace. It offers a complete cloud-based email service for sending, receiving and tracking email sent through your websites and applications. Mailgun features are available through an intuitive RESTful API or using traditional email protocols like SMTP.

Rackspace Cloud customers can send 50,000 emails per month for free with Mailgun's basic plan. As of July 2013, Rackspace pre-configures Windows and Linux images for Cloud Servers with Managed Operations to send email via Mailgun using SMTP.

The power of Mailgun comes in its wide array of usecases. Mailgun is capable of integrating with a local postfix service, sending email through SMTP and integrating programmatically with your existing cloud applications. For example, Mailgun, when properly configured, can deliver emails from a blogging software content management system (CMS) such as WordPress, integrate with bug tracking software or any open source software needing to send emails. Mailgun can also be used to handle incoming email.

Rackspace Cloud customers can access Mailgun from directly within the cloud control panel as pictured below. To send more than the free 50,000 emails/month with the basic plan, you will need to pick a paid plan in the Mailgun control panel.

For more information on Mailgun pricing for Rackspace customers, please visit their Rackspace pricing page.

This article will provide example configurations for Mailgun. To get the most from your services with Mailgun be sure to view the advanced resources available in the Further reading section.

On this Page:

Relaying with Postfix

Configuring Mailgun as a mail relay will allow Postfix to forward emails destined for remote delivery. Before we begin, ensure Postfix is installed and running on the server.

Note: Our knowledge center contains an article that details the Postfix installation process here.

To configure Mailgun we need to modify Postfix configuration options. Postfix configuration options are detailed in the file.

Open the file with the text editor of your choice. In this example we use vim .

vim /etc/postfix/

The relayhost parameter specifies the default host to send mail for remote delivery. Here we modify the value to include the Mailgun smtp server hostname and port as shown below.

relayhost = []:587

Postfix will now forward external email requests to the Mailgun server.

Next, we configure smtp authentication for the requests. A simple access and security layer (SASL) module handles this in the Postfix config. Modify the following SASL settings.

smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = static:username:password
smtp_sasl_security_options = noanonymous

Note: You will use the username and password from the Mailgun control panel.

SSL/TLS support (Optional)

Modify the following configuration options to include SSL/TLS support.

smtp_tls_security_level = may
smtpd_tls_security_level = may
smtp_tls_note_starttls_offer = yes

When using TLS encryption, make sure Postfix knows where to locate the Certificate Authority (CA) database for your linux distribution:

smtpd_tls_key_file = /etc/ssl/private/smtpd.key
smtpd_tls_cert_file = /etc/ssl/certs/smtpd.crt
smtpd_tls_CApath = /etc/ssl/certs

Revisit the relayhost parameter and configure the SSL/TLS port.

relayhost = []:465


Reload the Postfix service to load the modifications made in

sudo /etc/init.d/postfix restart

Postfix will now relay outbound emails to Mailgun.

Sending emails via SMTP

Integration with Mailgun programatically is available from languages with an SMTP library. Let's take a look at a trivial Python script that instructs Mailgun to send an email through SMTP. We will leverage the smtplib library available natively in Python.

import smtplib

def send_message_via_smtp():
    smtp = smtplib.SMTP("", 587)   
    smtp.login("", "password")
    smtp.sendmail("", "", "mime_message_body")


We begin by importing the smtplib package with import.  We then create a Python function to contain the code. Inside we create an object named smtp which is an instance of SMTP. In the login method we pass our mailgun SMTP credentials. In the sendmail method we define the sender, recipient, and email body data. We end the script calling the send_message_via_smtp function.

Note: You will use the username and password from the Mailgun control panel.

Sending emails via HTTP

SMTP is the most common protocol used when sending email. It is used by most email applications. However, SMTP may not be the first choice for coding due to MIME format underpenings. Sending via HTTP provides a simpler approach. With HTTP the application simply posts the content as parameters to Mailgun. The parameters match those required when sending a normal email: “From”, “To”, “Cc”, “Bcc”, “Subject” and so on. 

Let's take a look at a trivial cURL example. 

curl -s -k --user api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0 \ \    
-F from='Excited User <>'\    
-F subject='Hello'\    
-F text='Testing some Mailgun awesomeness!' 

The above example sends an email to two different addresses, with a subject of "Hello" and body "Testing some Mailgun awesomeness!". The HTTP API supports features such as file uploads, sending in test mode and more. See the API Documentation for more information.

Receiving messages via HTTP

Mailgun is able to receive email via rules you define. This is performed by generating routes within the Mailgun control panel or through the API. Once added, emails reaching Mailgun, for which a route is generated can be forwarded to your application via the use of an HTTP POST or your inbox. The screenshot below presents an example route from the Mailgun control panel.

Routes can be created for static email addresses and for a regular expression (regex) pattern match. They are also available to forward an HTTP request, for sending the email to your application. See Sending Documentation for more information.

Further Reading

This article included a few possibilities available with Mailgun. The mailgun website provides extensive API Reference material for multiple languages. Below are a few links that will serve to enchance your experience using the product: 

For support with the Mailgun product, please contact Mailgun directly at:

© 2015 Rackspace US, Inc.

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License

See license specifics and DISCLAIMER