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. )
5 Answers
To answer my own question, using postfix this is possible. You have to do two things:
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 =Using the following answer on serverfault ( ), I added the following (note that you need the postfix-pcre package installed):
In /etc/postfix/main.cf:
mydestination = pcre:/etc/postfix/mydestinationsIn /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.
"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 - - - - - local2.) 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/virtual4.) Add the following line to /etc/postfix/main.cf
virtual_alias_maps = regexp:/etc/postfix/virtual5.) Restart postfix:
sudo service postfix restartNow 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.
1See 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/sendmailAdd 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 restartNow 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
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.
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
Assuming
/etc/hostnamecontainsMyDevMachine; I ensure that/etc/hostscontains an entry127.0.1.1 MyDevMachineWhen I install Postfix, I would choose Local only, then for the domain setting, I make sure
MyDevMachineis set.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 todevuserI then run
sudo postmap /etc/postfix/vmapsthat eventually createsvmaps.db(sort of activating that virtual mapping).Locate the file
/etc/postfix/main.cfand append it with following line of code (which will direct Postfix to use the virtual mapping created earlier):virtual_alias_maps = regexp:/etc/postfix/vmapsRestart Postfix
sudo service postfix restart