Local only sendmail that delivers all mail to a directory

I have a development Ubuntu Server (12.04.1) VM that I use to develop php sites and e-commerce sites (like Magento etc).

Because this is for development purposes, and I need to test with e-mail addresses other than my own, I need a way for the sendmail command to accept a mail but not actually deliver it, but rather archive is somewhere where I can look at it.

What is the easiest way to install a mail server such that the sendmail command routes ALL mail to a local directory? i.e. I do NOT want any mail to be delivered to the internet.

I tried the steps discussed in this question ( SMTP server to deliver ALL mail to user@localhost), but it doesn't seem to work - I get an error message in my (local) mailbox telling me that it cannot deliver my test message (e.g. [email protected])


Solution 1:

To answer my own question, using postfix this is possible. You have to do two things:

  1. Add the configuration options discussed in SMTP server to deliver ALL mail to user@localhost, add the following to your /etc/postfix/main.cf file:

    luser_relay = MYLOCALUSER@localhost
    local_recipient_maps =
    
  2. Using the following answer on serverfault ( https://serverfault.com/questions/133190/host-wildcard-subdomains-using-postfix), I added the following (note that you need the postfix-pcre package installed):

    In /etc/postfix/main.cf:

     mydestination = pcre:/etc/postfix/mydestinations
    

    In /etc/postfix/mydestinations

    /.*/         ACCEPT
    

This will deliver ALL mail that postfix handles to the configured local user defined in #1, so don't use this for anything other than development.

Solution 2:

"Because this is for development purposes, and I need to test with e-mail addresses other than my own, I need a way for the sendmail command to accept a mail but not actually deliver it, but rather archive is somewhere where I can look at it."

You'll still need to deliver it. You just need to override "where" it's delivered.

"What is the easiest way to install a mail server such that the sendmail command routes ALL mail to a local directory? i.e. I do NOT want any mail to be delivered to the internet."

This can actually be done using only the default postfix installation package (no need for postfix-pcre).

1.) Following a tutorial here, edit 2 lines in the /etc/postfix/master.cf file to prevent any mail from being delivered externally (it gets stuck in the local mail queue):

smtp      unix  -       -       -       -       -       local
relay     unix  -       -       -       -       -       local

2.) Create a file in /etc/postfix called virtual. Place the following line within that file, replacing <USERNAME> with the local user account name you want all mail to be delivered to:

/.*/    <USERNAME>

3.) Run the following command to create the correct database file for postfix to look-up this new "virtual alias map". The new, autogenerated database file will be called "virtual.db"

sudo postmap /etc/postfix/virtual

4.) Add the following line to /etc/postfix/main.cf

virtual_alias_maps = regexp:/etc/postfix/virtual

5.) Restart postfix:

sudo service postfix restart

Now all mail, regardless of the sender, recipient or program that points to this SMTP server, will be delivered locally to the specified user. There are numerous options to read / retrieve these messages now. If you install an IMAP or POP3 courrier (such as dovecot), you can use a Mail User Agent (Thunderbird, Outlook, etc.) to connect to your local mailbox and read the messages.

Solution 3:

See another cool approach here How to catch emails sent with PHP on your local server.

$ sudo mkdir /var/log/mail
$ sudo nano /usr/local/bin/sendmail

Add following PHP script to this new "sendmail" file:

#!/usr/bin/php
<?php
$input = file_get_contents('php://stdin');
preg_match('|^To: (.*)|', $input, $matches);
$filename = tempnam('/var/log/mail', $matches[1] . '.');
file_put_contents($filename, $input);

Add sendmail_path = /usr/local/bin/sendmail to /etc/php5/apache2/php.ini.

$ sudo chmod 755 /usr/local/bin/sendmail
$ sudo chmod 777 /var/log/mail
$ sudo /etc/init.d/apache2 restart

Now all your mails are in /var/log/mail folder.

P.S. Also you might want to add shell_exec("chmod 777 /var/log/mail/ -R"); to /usr/local/bin/sendmail