Building a Test Email Server

This post explains how to set up an email server on GNU/Linux that can be used for testing applications. It allows you to test that emails are correctly addressed (except for BCC) and allows you to receive the emails and view them in any POP3 email client just as if they’d been received normally.

These instructions assume Ubuntu 12.04 LTS, and that we are installing our SMTP and POP3 server on the same computer that is running our application, and that the application uses the local SMTP relay.

First, install postfix, which will be your SMTP server.

sudo apt-get install postfix

When asked what type choose local only, it doesn’t matter what you choose as a domain name.

Next, add the following line to /etc/postfix/main.cf:

virtual_alias_maps = regexp:/etc/postfix/virtual

This sets up our use of virtual alias maps which we’ll use to redirect all the mail in the next step.

Then create the file /etc/postfix/virtual and add the following line to it:

/.*/ mailsink

We’ll need a user creating for this mail dump account so run the following:

sudo adduser mailsink

Be sure to give it a reasonable password as this will be used to access emails later.

You may also wish to block spam if the server is public facing, in which case add the following line to /etc/postfix/main.cf

smtpd_recipient_restrictions = reject_rbl_client zen.spamhaus.org, reject_rbl_client bl.spamcop.net

Now we need a POP3 server installing:

sudo apt-get install dovecot-pop3d

The following command enables clear text authentication over POP3:

sudo sed --in-place "s|#disable_plaintext_auth = yes|disable_plaintext_auth = no|" /etc/dovecot/conf.d/10-auth.conf

The following command sets the mail_location in Dovecot:

sudo sed --in-place "s|#mail_location = |mail_location = mbox:~/mail:INBOX=/var/mail/%u|" /etc/dovecot/conf.d/10-mail.conf

The following command ensures that Dovecot has permission to delete emails from the inbox:

sudo sed --in-place "s|#mail_privileged_group =|mail_privileged_group = mail|" /etc/dovecot/conf.d/10-mail.conf

Some e-mail clients will require you to specify an out-going SMTP server too. To make this easier you can enable SMTP access to this server. However, this will allow junk mail to be delivered to the test mailbox so avoid it if you can. To enable SMTP access run the following command:

sudo sed --in-place "s|inet_interfaces = loopback-only|inet_interfaces = all|" /etc/postfix/main.cf

Any mail sent using this SMTP server it will be routed to the mailsink in-box.

If you are using UFW then enable POP3 access:

sudo ufw allow pop3
sudo ufw allow smtp

Now restart the affected services:

sudo service dovecot restart
sudo service postfix restart

You can now configure your email client to access the server using POP3, you should configure it to leave messages on the server so that you can test using different email clients. If the server is accessible over the internet then you can configure GMAIL and HotMail to access the mail using POP3.

Run the following command to send a test email (to verify that all email addresses get re-routed just use a real email address that you have access to, if this email doesn’t reach that address then everything is OK):

echo $'Subject: TEST\nHELLO\n.\n' | sendmail -t here@there.com

Author: Phill Luby

Phill is a Software Developer and Co-founder of NewRedo Ltd.