Access the Mail Spool on OSX with Postfix and Dovecot

Published on 20 July 2016

If you're developing sites locally on OSX, you will undoubtedly need to send emails, and sometimes, even see what they look like. On a spanking new site with no other users, you could just get away with setting the recipient address to yourself and connecting an SMTP relay, or using your ISP or mail provider's SMTP creds. But if you're working with sample data from a live site, you want to ensure you don't accidentally spam your user-base or clients while you're testing.

On OSX, you can do this by redirecting all mail sent from your local site to your local user's mail spool through Postfix, set up a local IMAP service using Dovecot, then connecting a mail client to that IMAP server. It sounds a bit convoluted, but it's quite straight forward to set up.

Note: This setup has been tested on El Capitan, and has had a minor update for High Sierra.

Step 1. Configure Postfix

The first step will be to have Postfix redirect all mail to one address that you can control.

  1. Add the following line to /etc/postfix/main.cf: virtual_maps=regexp:/etc/postfix/virtual-redirect
  2. Create /etc/postfix/virtual-redirect with the following line, replacing username with your username on OSX. /.+@.+/ username
  3. Map this file to Postfix by running: sudo postmap /etc/postfix/virtual-redirect This will create a virtual-redirect.db file.
  4. Reload Postfix to run with your changes. sudo postfix reload

You can test that this has worked by triggering an email to send through your site or app, and viewing your mail spool.

Step 2. Set up a local IMAP server with Dovecot

A local IMAP server will act as a bridge between your mbox mail spool and a standard mail client.

Note: If you can find a mail client that still supports the mbox format, you can skip this step entirely. But most (free?) OSX mail clients do not seem to support it anymore. If you don't need a graphical interface to view your mails, you can use the terminal-based mail. Otherwise, proceed.

  1. If you haven't already, install Homebrew.

  2. Use brew to install Dovecot. brew install dovecot

  3. Copy the configuration files from the Dovecot example directory. cp -pr /usr/local/Cellar/dovecot/2.2.24/share/doc/dovecot/example-config/ /usr/local/etc/dovecot/ Note: This path may be a little different based on the version you have installed. If you can't find the example-config directory, check /usr/local/etc/dovecot/README for the correct location.

  4. Create /usr/local/etc/dovecot/local.conf with the following config. You will need to replace capitalised sections with your own information.

     \# Listen for localhost
     listen = 127.0.0.1
    

    Use IMAP

    protocols = imap
    

    Set a password

    This is fine for local development, not a proper server.

      driver = static
      args = password=YOURPASSWORD
    }```
    
    # Set the mail location. %u will be substituted with your username.
    # The first path is where your other IMAP folders will go,
    # the second is where your mail spool is.
    # See dovecot/conf.d/10-mail.conf for more information.
    #mail\_location = mbox:/Users/%u/mail:INBOX=/var/mail/%u
    mail\_location = YOURMAILLOCATION
    
    # Set the user and group for accessing mail.
    # The groupname might be staff, root or admin depending on how
    # your computer is set up.
    mail\_uid = YOURUSERNAME
    mail\_gid = YOURGROUPNAME
    
    # Login user is internally used by login processes. This is the most
    # untrusted user in Dovecot system. It shouldn't have access to anything
    # at all.
    default\_login\_user = \_dovenull
    
    # Internal user is used by unprivileged processes. It should be separate
    # from login user, so that login processes can't disturb other processes.
    default\_internal\_user = \_dovecot
    
    # Group to enable temporarily for privileged operations. Currently this is
    # used only with INBOX when either its initial creation or dotlocking
    # fails. Typically this is set to "mail" to give access to /var/mail.
    mail\_privileged\_group = mail
    
    # Needed this additional assignment for High Sierra
    default\_internal\_group = mail
    
    
  5. Update the following Dovecot config files:

    1. /usr/local/etc/dovecot/conf.d/10-auth.conf Comment out the !include auth-system.conf.ext line.
    2. /usr/local/etc/dovecot/conf.d/10-ssl.conf Set ssl = no, and comment out the ssl_cert and ssl_key lines.
  6. Update the perms on your mail spool with: chmod +t /var/mail/YOURUSERNAME

You can start dovecot with sudo brew services start dovecot, or restart it with the restart directive.

Step 3. Connect your mail client

Use your username, and the password you set in the Dovecot config file with localhost as your IMAP server, and the messages in your spool should load. It may give you a warning that it could not connect securely, but that's because it was configured that way.

Now go forth and edit those email templates, or whatever it is you were doing that needed this.