Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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. someone@gmail.com)

share|improve this question

2 Answers 2

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 ( http://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.

share|improve this answer

See another cool approach here http://martinvalasek.com/blog/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 mail 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

share|improve this answer
    
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 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 at 15:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.