ctors.net
Blog tvlooy

Dealing with e-mail in a development environment

Dovecot, Postfix, Roundcube | January 18, 2013

This article was written for Intracto and first appeared on the Intracto blog on January 18, 2013.

If you build web applications, chances are that they will send e-mail. Think contact forms, order confirmations, etc. On a developer machine however, you usually don't want this e-mail to be delivered to a real mailbox. You could use MailCatcher to solve this problem, but there are also other solutions, like using your current MTA in a smart way.

MailCatcher

MailCatcher catches all e-mail sent on a system, and stores it for display. You can read from the website: "MailCatcher runs a super simple SMTP server which catches any message sent to it to display in a web interface. Run mailcatcher, set your favourite app to deliver to smtp://127.0.0.1:1025 instead of your default SMTP server, then check out http://127.0.0.1:1080 to see the mail that's arrived so far."

This behaviour is useful on a developer machine, where you want to send e-mails, but you don't want them to arrive in your mailbox, or in your spam filter, or even worse, in a real customer's mailbox. The downside is that, out of the box, you are running an extra service on port 1080 and you have to deliver the e-mail with SMTP on port 1025. That means you need to fiddle with your project's e-mail settings, or with the php.ini.

Postfix

Wouldn't it be great if you could just tweak your current MTA a bit to catch all e-mail and deliver this to a default mailbox on the system? Sure! At Intracto we use Vagrant with Puppet for some projects and we choose to keep using our well-known Postfix to handle e-mail. It’s easy to configure Postfix to make sure no e-mail leaves the box. Instead, all e-mail, regardless the sender or the receiver, will be delivered to the local vagrant user. The mailbox of the vagrant user can be read with a combination of Dovecot and Roundcube. Internally, we call this an e-mail collect. This is a clean and simple solution and there is no need to fiddle with your e-mail settings.

The way we set up the e-mail collect for our SecretSanta project can be found on GitHub. To summarize the setup, we install packages 'postfix', 'postfix-pcre', 'dovecot-imapd' and 'roundcube-core'. The following lines were added to the default Postfix /etc/postfix/main.cf:

virtual_alias_domains = virtual_alias_maps = pcre:/etc/postfix/virtual_forwardings.pcre virtual_mailbox_domains = pcre:/etc/postfix/virtual_domains.pcre home_mailbox = Maildir/

Then, we use pcre (Perl Compatible Regular Expressions) to configure virtual aliases:

echo "/@.*/ vagrant" > /etc/postfix/virtual_forwardings.pcre echo "/^.*/ OK" > /etc/postfix/virtual_domains.pcre

Dovecot works out of the box and Roundcube was enabled on /roundcube in Apache2. You can then just use the default vagrant user to log into the mailbox. We did change the following settings in the default Roundcube /etc/roundcube/main.inc.php to make it a bit easier to work with:

$rcmail_config['default_host'] = array('localhost'); $rcmail_config['list_cols'] = array('subject', 'from', 'to', 'date', 'size', 'flag', 'attachment'); $rcmail_config['show_images'] = 2; $rcmail_config['create_default_folders'] = TRUE;

Conclusion

While I think MailCatcher is a very cool project and our Puppet modules are still a work in progress, we are very happy with our Postfix / Roundcube setup right now and we don't plan to replace it anytime soon. For now, I just liked to share this small insight about our development and deployment setup. I'm always curious for your thoughts and your experiences, so please feel free to leave a comment, or talk to us at Puppet Camp 2013 Ghent which we will most likely be attending.