Emails: Why is sending them so hard?

Since I like to think I’m pretty handy with computers, I like to have one of them running all the time. To that end, I bought an HP ProLiant Microserver some time ago, and always keep it on. It’s a great little low-power server, and I use it for things like storing all my files so I can access it from computers around the house, automatically making on- and off-site backups every day, allowing access to my home networks, etc.

This setup is fairly straightforward, but there is one considerable pain point: The server needs to send email. It’s currently using ZFS with RAID-Z to store my files, and I would like to be notified if there are some disk errors so I can replace the disks. The system can easily send emails, but having those emails actually get delivered and not go into spam is a very different matter.

Mail nowadays: Not so easy.

If you’ve ever tried to run your own mailserver, you know that ensuring deliverability is hard. Mail servers on the internet today are very suspicious of new servers, due to the spam problem, so if you just start sending email from your new server and your home connection, one of two things will happen: Best case, it will end up in your spam inbox, and, worst case, it will be blocked and you’ll never hear about it.

The consequences of this can range from the inconvenient to the disastrous, depending on how important the email that was being sent is, so it’s easy to see that our server’s sending capabilities have to be as robust as possible. However, this isn’t a production mail system in a large application, so how can we solve the problem without spending a lot of time on it? Well, there’s a very easy way.

Fortunately, there are many excellent email-sending services nowadays, and which can be integrated with no hassle. I’ll show you how you can do it below (my server runs Ubuntu, but it should be very similar on any *nix-based server).

Installing a mail agent

First of all, we need a mail agent. This is a program that runs on your server to accept emails and sends them. You can use something like postfix or sendmail, but I find them too complicated for something that just needs to send a few messages per month, plus you have to make sure you secure them so that you don’t allow random people to send messages from your server. Due to these considerations, I use nullmailer, a very simple MTA that needs almost no configuration.

Let’s begin by installing it:

sudo apt-get install nullmailer

Now that we’ve gotten past the hard part, all that’s left is configuring it. To do that, we’ll first need a remote server that will actually want to send our emails, and this is where the next part comes in.

Using the mail-sending service

Nullmailer doesn’t actually deliver emails to its destination, it merely relays them to a server that can send them. This is where the email service I mentioned above comes in.

For this part, I use Mailgun. It’s a very solid service, very developer-friendly, with great deliverability and UI. Their free quota is 10,000 emails per month, which is much, much more than the 5 or so I need. Head over to them and sign up right now, add your domain and they will give you a username and password so you can send mails through them. It will also give you instructions about adding DomainKeys and SPF records to your domain, which you should do before continuing.

That’s the other hard part done, and we can continue to the third hard part, which is “configuring nullmailer”.

Configuring the mail agent

To configure nullmailer, all you have to do is edit a single file. Let’s do that now:

sudo nano /etc/nullmailer/remotes

You can use nano, or you can use ed, like a real geek. The file’s contents should be the following, to tell it which server it can connect to and how:

smtp.mailgun.org smtp --port=587 --starttls --user=your_mailgun_username --pass=your_mailgun_pass

Save the file, restart nullmailer for good measure (I’m not sure that’s required, but it doesn’t hurt), and you’re done:

sudo /etc/init.d/nullmailer restart

Trying it out

After all this, your server should be correctly configured to send mail. Let’s try it out:

echo "Hello self! I hope you get this."  | sendmail youremail@example.com

If everything is working properly (and it will be, because my instructions were awesome), you will receive the email in a few seconds. For super-extra-improved deliverability, you can change /etc/mailname to the domain you entered on the mailgun interface, and you should be good to go.

This probably took you around five minutes to complete, and now you can be sure that you won’t miss any emails from your server any more! Many thanks go to to nullmailer and mailgun for the excellent software and services.

If you have any feedback on this process or just want to share your own server build, leave a comment below or get me on Twitter. To be notified of my posts, subscribe to my mailing list below!