9

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])

5 Answers 5

8

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.

6

"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.

1
  • Make sure that the <USERNAME> is mentioned in etc/aliases, otherwise you will get errors. Also, if you want to use a Maildir for that user, you must add the lines home_mailbox = Maildir/ and mailbox_command = to your /etc/postfix/main.cf
    – chiborg
    Feb 11, 2016 at 12:34
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

2
  • The only problem with this solution is that you cannot use whatever mail client you have installed to read the mail as the script just dumps the mail in a directory. Being able to serve the mail up via pop3 or imap for example, lets you test your mail in actual mail clients.
    – Gareth
    Jan 25, 2014 at 13:32
  • Thank you. I tried your solution too. It worked, thanks! But when I used it I was able to see only emails of user with hostname of MYLOCALUSER. But I wanted to test emails that sent do different accounts.
    – milkovsky
    Jan 25, 2014 at 15:26
0

Another approach is to run a "FakeSMTP" server, a cross platform SMTP handler; who's whole purpose is to receive emails, store and display them with your configured email client.

FakeSMTP Screenshot

0

I have exactly a similar environment. But most online documentation about setting up Postfix advise you to choose Internet Site during the installation process. I think this is an overkill for a local development server on my machine only. So for me, I do the following

  1. Assuming /etc/hostname contains MyDevMachine; I ensure that /etc/hosts contains an entry 127.0.1.1 MyDevMachine

  2. When I install Postfix, I would choose Local only, then for the domain setting, I make sure MyDevMachine is set.

  3. Because I want all emails to be sent to my the development user, and assuming the name of this development user on Ubuntu is devuser; I therefore create a file called /etc/postfix/vmaps. The content of this file is /.*/ devuser. This would allow all emails to be delivered to devuser

  4. I then run sudo postmap /etc/postfix/vmaps that eventually creates vmaps.db (sort of activating that virtual mapping).

  5. Locate the file /etc/postfix/main.cf and append it with following line of code (which will direct Postfix to use the virtual mapping created earlier): virtual_alias_maps = regexp:/etc/postfix/vmaps

  6. Restart Postfix sudo service postfix restart

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .