What is Mailgun? Mailgun is an email automation service acquired by Rackspace in 2012. 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.
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 programatically 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. By selecting the Mailgun option, you'll automatically be able to send 50,000 emails/month for free. To send more than 50,000 emails/month, you will just need to pick a paid plan in the Mailgun control panel and enter your credit card information."

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:
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 main.cf file.
Open the file with the text editor of your choice. In this example we use vim .
vim /etc/postfix/main.cf
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 = [smtp.mailgun.org]: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:postmaster@mydomain.com:password
smtp_sasl_security_options = noanonymous
Note: You will use the username and password from the Mailgun control panel.
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 = [smtp.mailgun.org]:465
Reload the Postfix service to load the modifications made in main.cf.
sudo /etc/init.d/postfix restart
Postfix will now relay outbound emails to Mailgun.
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("smtp.mailgun.org", 587)
smtp.login("username@example.com", "password")
smtp.sendmail("from@example.com", "to@example.com", "mime_message_body")
send_message_via_smtp();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.
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 \
https://api.mailgun.net/v2/samples.mailgun.org/messages \
-F from='Excited User <me@samples.mailgun.org>'\
-F to=tom@example.com\
-F to=mary@example.com\
-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.
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.
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:
© 2011-2013 Rackspace US, Inc.
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License

4 Comments
Using SSL in Postfix
Also - it would be helpful to post instructions for testing.
Thanks for the post.
re: Install and testing
If you don't have a program you're looking to test the mail functionality with you can use the "mail" command, as in:
mail -s "Mail test" name@example.com
After hitting enter you can type a message, then hit Control-D to send the message.
If you get an error saying that the mail command isn't available then you may need to install the "mailx" package, if I remember the name right for CentOS.
What About Send Grid
re: SendGrid
http://www.rackspace.com/cloud/tools/
Add new comment