MailGun as an SMTP Server for Django Apps

Gmail is a frequently used SMTP host for Django apps.  Its free and relatively simple to setup which is ideal for small apps.  There are a few significant downfalls. First, its limited to 500 emails a day with no option to upgrade.  Second, it limits and even re-writes all outgoing emails to the username of the connector.

For example if you’re trying to send an email from or you’re out of luck because all of those emails will actually get sent at or whatever email account you have configured. I’m sure this saves millions of people from millions of spam but it adds a bit of annoyance to developer’s lives.

The recently launched MailGunoffers a great solution.  Its free for up to 200 emails per day and super cheap and dramatically scalable after that.  It also works as a drop in replacement for whatever service you were using for your django smtp server.  Changing will take you less time than reading this article.

The Steps:

  1. Sign up for MailGun
  2. Go to the control panel and click on the server created for you.
  3. In the upper right you will find your “SMTP Authentication” credentials for this server.
  4. Open the in your django app and configure your email with the given credentials.  It should look something like this.
    EMAIL_USE_TLS = True
    EMAIL_HOST = ''
    EMAIL_PORT = 587
  5. Test it out!./ shell

    >>> from django.core.mail import send_mail
    >>> send_mail('MailGun works great!', 'It really really does.', '', [''], fail_silently=False)

  1. I noticed you’re using TLS here. The nice thing about it, besides protecting your SMTP credentials via encryption, is that port #587 is almost never blocked by ISPs, therefore your code will continue working on your laptop even when running from your home.

    • wrichert said:

      What I especially like about MailGun is that their API allows to send individual mails. E.g. MailChimp would not allow a Django use case that you outlined above really nicely.

  2. Yashh said:

    Dave you should use Amazon SES. I think they give you 2000 free per day and $0.10 per thousand. And its pretty easy to use with boto 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s