DevOps Zone is brought to you in partnership with:

A friendly Finnish hacker. I am technology consultant, open source advocator and entrepreneur. My expertise areas cover HTML5, Python, Plone, Javascript, WebGL,UNIX and mobile web. Mikko likes sushi, Angry Birds and dislikes winter. Mikko is a DZone MVB and is not an employee of DZone and has posted 43 posts at DZone. You can read more from them at their website. View Full User Profile

Using Nullmailer and Mandrill for Your Ubuntu Server Outboud Mail

03.25.2013
| 6566 views |
  • submit to reddit

Getting a good SMTP server for your small business web application is difficult. You usually rely on the your local VPS server provider having some kind of SMTP server in local network. Because email handling might not be part of the core competence of your hosting provider, the provided service, if there is any, is usually ridden with problems. An alternative is buying a premium SMTP server, but pricing plans do not always meet the provided documentation, features and API support.

This all can change today. Meet Mandrill. Mandrill is outgoing email service provided by Mailchimp email newsletter service – so they have all the money, skill and interest keep their SMTP server working and spam blacklist free. This blog posts discuss how to use Mandrill as Nullmailer backend, so that your server can send easily email through Mandrill SMTP.

1. Benefits of Mandrill

Mandrill is

  • Real SMTP server with SMTP and HTTP APIs
  • 12k free emails per month – very generous from them
  • The owner has clear interest keeping it spam blacklist free (Mailchimp, their main product, is a newsletter service)
  • The web admin interface is easy and powerful
  • Mandrill supports converting all links in emails automatically trackable

Sidenote: when dealing with setting From: address in email make sure the domain records of the from email address whitelist the outgoing email server IP addresses via SPF records

2. Benefits of Nullmailer

Nullmailer provides sendmail compatible email out interface on your server.

You want to use Nullmailer because

  • Super easy to set-up
  • Provides local queue
  • Lean, unlike heavy-weight Linux mail relay solutions (Exim 4, Postfix, Sendmail)

Note: Nullmailer does not provide local SMTP server listening to port 25. I’ll blog about how to deal with this later.

3. Setting up Nullmailer + Mandrill for Ubuntu 12.04 LTS

Nullmailer is a mail command emulator, as discussed before. A lot of old fashioned web applications, like ones written in PHP, don’t have good options to set outgoing email server directly. Thus, you need to setup your local server to be a Mandrill mail slave. As a bonus, your mail UNIX command starts working also, getting support for Cron etc.

4. Install Nullmailer

You want to have your email traffic to be SSL encrypted. Nullmailer supports this since version 1.10 . Ubuntu 12.04 ships with Nullmailer 1.05-1. To get the latest Nullmailer for Ubuntu do from my Ubuntu PPA (see prior blog post about how I backported Nullmailer to Ubuntu 12.04)

Note: This will remove existing mail solutions on the server.

sudo apt-add-repository ppa:mikko-red-innovation/ppa
sudo apt-get update
sudo apt-get install nullmailer=1:1.11-2~precise1~ppa1

(Read more about my Nullmailer backporting effort).

5. Setup email relaying and information

Setup Mandrill upstream SMTP in /etc/nullmailer/remotes:

sudo echo "smtp.mandrillapp.com smtp --port=587 --starttls --user=xxx@example.com --pass=ccc" > /etc/nullmailer/remotes"

Setup domain info:

sudo echo example.com > /etc/nullmailer/defaultdomain
sudo echo "\$hostname.example.com" > /etc/nullmailer/me

Add yourself as an admin of outgoing mail:

sudo echo "mikko@example.com" > /etc/nullmailer/adminaddr

Make Nullmailer to read new configs

service nullmailer reload

6. Testing Nullmailer

Below is the command line to send some mail to yourself:

echo "This is a test message from ${USER}@${HOSTNAME} at $(date)" \
  | sendmail mikko@example.com

You can see Nullmailer log:

tail -f /var/log/mail.log /var/log/mail.err
Mar 20 16:30:45 koskela nullmailer[17645]: Trigger pulled.
Mar 20 16:30:45 koskela nullmailer[17645]: Rescanning queue.
Mar 20 16:30:45 koskela nullmailer[17645]: Starting delivery: protocol: smtp host: smtp.mandrillapp.com file: 1363793445.17667
Mar 20 16:30:45 koskela nullmailer[17645]: Starting delivery, 1 message(s) in queue.
Mar 20 16:30:46 koskela nullmailer[17668]: smtp: Succeeded: 250 2.0.0 Ok: queued as 4A66419E03C
Mar 20 16:30:46 koskela nullmailer[17645]: Sent file.
Mar 20 16:30:46 koskela nullmailer[17645]: Delivery complete, 0 message(s) remain.

You also also check the server local mail queue

mailq

Mandrill itself provides very, very, nice activity dashboard for monitoring error situations

Screen Shot 2013-03-20 at 5.51.14 PM

Published at DZone with permission of Mikko Ohtamaa, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)