Single-machine dev mail setup

I sometimes work on websites where sending email is a central task. To be able to efficiently test my code on my Ubuntu development machine, I have created a simple setup from the following requirements:

  1. No mail should reach the internet
  2. I should be able to quickly read every message that is “sent” from PHP

The strategy is to use Postfix to catch and save messages (and enable mail() at all), and Mutt to read them without any fluff.

Postfix setup

Install it:

sudo apt-get install postfix

Most of the configuration of Postfix resides in /etc/postfix/main.cf. Add the following lines:

# Redirecting to local mailbox
home_mailbox = .mail/
virtual_mailbox_domains = regexp:/etc/postfix/domains.regexp
virtual_alias_maps = regexp:/etc/postfix/virtual.regexp

Also create the referenced files /etc/postfix/domains.regexp:

/.*/ localhost

and /etc/postfix/virtual.regexp:

/.*/ arildm # Your username

Reload the configuration:

sudo postfix reload

Here we’re telling Postfix to be the end destination for all mail and redirect it to the local user arildm (which is me).

For home_mailbox, many guides that I found use Mailbox/. I suppose that’s more or less standard, but I don’t like unnecessary capitalisation nor non-hidden directories in my home directory (because that’s where the mail has to go if you use maildir on Ubuntu, as far as I know). By ending the parameter value with a slash, we tell Postfix to use the mailbox format rather than mbox. That has the advantage that each message is written to its own file, which facilitates manual inspection a lot.

Test the configuration:

echo I am a mail | sendmail recipient@example.com
find ~/.mail

Mutt

Mutt is a console email client. Install it:

sudo apt-get install mutt

Create ~/.muttrc and add:

set mbox_type=maildir
set folder="~/.mail"
set mask="!^\\.[^.]"
set mbox="~/.mail"
set spoolfile="~/.mail"

If I understood correctly, by default Mutt reads mail from the spool (usually /var/mail/$USER) and moves read messages to ~/mbox. The lines above sets both these paths to ~/.mail. They also tell Mutt to assume the maildir format and to ignore hidden files.

Now run Mutt and expect to see the previously sent test message.

mutt

Mutt navigation at a glance:

q
quit
j
next message
k
previous message
<enter>
open message
h
show headers (whole raw message)

Leave a Reply

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