Dev-Box: Installing a Mail Server

It’s been a while, but I’m back with another useful tutorial for all those out there using VMs like my Dev-Box. Last time I talked about getting SVN to work on your VM and now time it’s all about setting up a mail server, something you need for pretty much every web application you might develop.

As my posts are meant for users that use VMs for local development, I’ll not tell you how to install a usual mail server, but rather how to configure it that way, that all outgoing emails go to one email address instead of the address they are meant for. Using such a catch-all setup allows you to test emails coming from your app without having to send mails to existing accounts and even allows you to use your live database for testing and not having to worry that the live users might get an email that’s only meant for testing.

Installation

Installing the mailing software is actually pretty easy, just run the below command and select the “No Configuration” option when asked how you want your server to be configured. We’ll take care of this ourself.

apt-get install dovecot-postfix

Configuration

The configuration is where it gets kinda tricky. The way we are gonna set it up, all emails send by your server will be redirected to a single email address you define. In my case I’ll use an gmail account that all my the emails from my VM will be send to but in theory you could use any email account with any provider. So let’s get it started and I’ll explain further as we get along:

First we add a rule that makes sure all emails are redirected to a single email address. We do this by creating a new file called recipient_canonical_map

cd /etc/postfix
nano recipient_canonical_map

with this content

/./ gmail_username@gmail.com

Once you saved the file we open up the postfix main config file

nano main.cf

and enter this content

recipient_canonical_classes = envelope_recipient
recipient_canonical_maps = regexp:/etc/postfix/recipient_canonical_map

Now we can continue to the second part of the configuration where we make sure all the outgoings email are actually send and are send to your email address. We open up the main.cf file again

nano main.cf

and add the following lines

# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=no
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
# information on enabling SSL in the smtp client.

myhostname = vidyadhar.home.network
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination =
relayhost = [smtp.gmail.com]:587
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = loopback-only
default_transport = smtp
relay_transport = smtp
inet_protocols = all

# SASL Settings
smtp_use_tls=yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem

The SASL settings make sure that your server won’t be banned for spamming the mail account. Not sure if this works with other providers as well, only tested with Google Mail. To make the SASL part work we have to create a password file:

nano sasl_passwd

insert the following line (where you have to replace the email address and the password with your actual credentials)

[smtp.gmail.com]:587   gmail_username@gmail.com:gmail_password

Now we secure file and validate our certificate:

chmod 400 /etc/postfix/sasl_passwd
postmap /etc/postfix/sasl_passwd
touch /etc/postfix/cacert.pem
cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem >> /etc/postfix/cacert.pem

After restarting the mail server we are done and you can send an email from one of your apps and it should show up in your inbox of your gmail account:

/etc/init.d/postfix restart

I hope you like my mail server setup and it works for you :) I didn’t test this as extensive as I wanted to, but it works for me and so I hope it does for you! I’m looking forward to your comments and critique.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.