Gareth Lennox

Delivering all mail to a local account

I recently asked a question on Ask Ubuntu, regarding setting up my Ubuntu (12.04) virtual machine with a mail server, but to not actually send any mail.

What’s the point of this? Well, because I’m developing web applications that send mail, I want to test with all sorts of different e-mail addresses, other than my own, without actually sending these test messages. Ideally all the messages would get dumped into a single folder to me to review whenever I need to.

I didn’t get a response (yes, I’m impatient!), but managed to work it out myself, and posted the answer to the site. I’m putting it here for future reference. I used the postfix mail server, which is easy to install (sudo apt-get install postfix) and configure. To get it to deliver all mail locally, you need to do two things:

  1. Install the postfix-pcre package (sudo  apt-get install postfix-pcre)
  2. Edit the main postfix configuration file (sudo nano /etc/postfix/main.cf file) and add the following lines, tweaking the MYLOCALUSER value to a user on the machine (using an answer on AskUbuntu):
    luser_relay = MYLOCALUSER@localhost
    local_recipient_maps =
  3. Still editing the file, replace the mydestination configuration value using the value below (using an answer on serverfault) (note that you need the postfix-pcre package installed):
    mydestination = pcre:/etc/postfix/mydestinations
  4. Create and edit the mydestinations file referenced above (sudo nano /etc/postfix/mydestinations) and put the following line in it:
    /.*/ ACCEPT
  5. Restart the postfix server:
    sudo /etc/init.d/postfix restart

This creates a rule that basically tells postfix to treat all mail it processes as local domains. The luser_relay value and the local_recipient_maps value work together to define the default user to deliver mail to, your local user.

You can thereafter use whatever mail client you want, as all mail the server deals with is routed to your local user. Obviously you shouldn’t ever do this on a server that you want to, you know, actually send mail or anything, therefore it is primarily useful when developing software and you want to test you mail sending code.

 

 

Comments are closed.