Set up Postfix with Mailgun for reliable e-mail delivery

  • Install Postfix

    The next step in our guide is to get Postfix installed - Postfix an STMP/Email server that is used for delivering email. It is NOT a POP3/IMAP server, so it's not something you will use to get email into your mail client.

    As the root user, install postfix:

    apt-get install postfix
    

    This will install the postfix Mail server, with it's config files located in /etc/postfix.
  • Choose "Internet Site" and enter, then enter your domain name on the followup screen.
  • Signup for a free Mailgun account

    The next step is to get setup with a free Mailgun account. What is Mailgun? From the Mailgun website:

    "Mailgun is a set of powerful APIs that allow you to send, receive and track email effortlessly.

    Think of Postfix as the mailbox at your house - when you want to send a letter (email) to someone else, you put it in the mailbox (Postfix). Then the mail is picked up by the mail carrier (Mailgun) and delivered.

    Mailgun is optional - you can send and receive email directly using Postfix, however I would highly recommend using Mailgun as your delivery service, as they will get your email delivered to a higher percentage of your customers than delivering on your own.

    The first 10,000 emails each month via Mailgun are free - after that it is just 50 cents for every 1,000. You can signup for an account here:

    http://www.mailgun.com/

    Once signed up, go to your Mailgun Control Panel:

    https://mailgun.com/cp

    and click on the "domains" tab then the big "Add Domain" button:
  • Enter your domain name on the form that comes up and press the dark gray "Create Domain" button at the bottom of the form.

    This will create your domain in Mailgun.

    Next, click on the "Domains" tab again, and then click on your new domain in the domains list. This will take you into the details page for your new domain, and should look something like the following:
  • Configuring Postfix to use Mailgun as a relay host

    On this screen you can find your SMTP password - this is the password we are going to need when configuring Postfix to send our email through Mailgun.

    So with the new password in hand, head back to your new Droplet and let's edit the Postfix main.cf file:

    nano /etc/postfix/main.cf
    

    and add the following to the bottom of the file:

    relayhost = smtp.mailgun.org
    smtp_sasl_auth_enable = yes
    smtp_sasl_password_maps = static:postmaster@jamroom.net:password
    smtp_sasl_security_options = noanonymous
    

    Replace:

    - jamroom.net with YOUR DOMAIN name
    - password with the password you just grabbed from the Mailgun control panel.

    Save your changes and restart postfix:

    service postfix restart
    

    That's it - your Postfix will now send all email out through Mailgun.
smith.kyle
26 Nov 2013 05:18:33PM @smithkyle:
Was looking through the settings for jrMailer and saw that you can use either php's built in mail function or a specified smtp server. Now that mailgun has been setup, should the mailgun smtp server be used? or can we leave it on php's mail function?
brian
26 Nov 2013 05:35:21PM @brian:
You actually want to leave that using the PHP function - there's no need to use the Mailgun module with this setup. By using the PHP function, mail will be forwarded to the Postfix daemon which can buffer and then work with Mailgun for delivery in the background.

The Mailgun module is designed for sites (such as a local dev site) where you're not running a local SMTP server like postfix.

Hope this helps!

smith.kyle
27 Nov 2013 12:24:36PM @smithkyle:
Ahh - thanks!
smith.kyle
05 Dec 2013 07:48:11AM @smithkyle:
Got another question on this! I setup everything per the instructions, including a route in mailgun to forward all mail from admin@mydomain to one of my other emails. Everything works fine except for when the system itself handles email to the domain. Because "admin" isn't also a user account on my server, when the system sends out mail or I test with "sendmail admin@mydomain," the mail gets delivered to root and isn't evening going to the relayhost and mailgun. I got around this by removing mydomain from the mydestinations directive in /etc/postfix/main.cf, but isn't that kind of a hack since no mail to my domain will now be delivered locally if I ever want it to be for another user? Is there a way to just have the admin@mydomain address go through the relayhost, but have all other @mydomain addresses go through the local server? Or is that kind of a corner case and not how things should really be set up?

Thanks!

Kyle

brian
05 Dec 2013 02:37:39PM @brian:
If you've setup the main.cf file to use Mailgun as the smart host, all mail should be routed out through Mailgun - are you using a full domain address? i.e. "amin@mydomain.com"?

I've got Jamroom.net setup this way and have not seen this before, so something may not be working right.

smith.kyle
05 Dec 2013 08:14:04PM @smithkyle:
Ahh - could be be because I have multiple relayhosts? This is what my main.cf looks like:

Quote: myhostname = localhost
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = mydomain.com, localhost, localhost.localdomain, localhost
relayhost =
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0] /104 [::1] /128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all

relayhost = smtp.mailgun.org
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = #mailgun stuff here
smtp_sasl_security_options = noanonymous

with that, if I do "sendmail my@gmail.com" it goes through fine, but "sendmail admin@mydomain.com" doesn't even get to mailgun. If i just send an email to admin@mydomain.com, though, it goes through so I know the route is working on mailgun.

brian
06 Dec 2013 05:21:32PM @brian:
Looks like you only have one - I believe the last one in the file "overrides" the empty first one. You could try removing mydomain.com from the "mydestination" line - I believe then your email should be routed out through Mailgun. Let me know if that helps.
smith.kyle
10 Dec 2013 11:02:52AM @smithkyle:
Yeah that worked - thanks! I spent a long time looking through the postfix docs trying to just have the one internal address get sent through mailgun instead of trying be delivered locally to a nonexistent account...then gave up because I decided that was really a corner case and wouldn't be setup like that really anywhere else lol. Thanks for your help!

Kyle

Tags