How to configure postfix to deliver all mail to one mailbox
October 19th, 2013
Warning: This post is 10 years old. Some of this information may be out of date.
I've been using Juan Treminio's excellent Puphpet tool to provision and set up a development server on the Macbook. Whilst it is an excellent tool for developing PHP websites it doesn't come set up with an email server. My needs were simple, I wanted my PHP websites to send email via the built in mail() function, or via SMTP, but not to send mail externally. Ideally I wanted the the server to deliver all mail to one mailbox. On a previous VM my colleague, Aaron Brady, had helped me configure Exim to deliver all mail locally but I found an alternative method using Postfix.
Here's how to configure Postfix to deliver all mail to one mailbox.
Installing postfix I'm using Ubuntu 12.04 server for my development so I used apt-get to install.
sudo apt-get install postfix
Whilst this is intalling you will be prompted to configure Postfix for your needs. The first screen will ask you to choose your server configuration type. For our needs we choose 'Local only':
Next you will be asked to enter the domain name used for any email address without the domain part. As we are configuring Postfix to deliver all mail to a local user we can simply use the hostname of the server and select 'OK'
Configuring Postfix to deliver all mail to a local user
Thats all that is needed to install and start Postfix. Now we need to configure it to deliver all mail to our local user. To do this we use Postfix's address rewriting. First, edit /etc/postfix/main.cf
and add the following at the bottom:
canonical_maps = regexp:/etc/postfix/canonical-redirect
Now create a new file at
/etc/postfix/canonical-redirect
and add the following. As I'm using a vagrant box I want all mail delivering to the 'vagrant' user. You should change this to the user you want mail delivering to:
/^.*$/ vagrant
Save the file and restart Postfix:
sudo service postfix restart
That's it! Mail should now be delivered to the Vagrant user. To test, run this one-line php script. You should see 'bool(true') as a result.
$ php -r "var_dump(mail('[email protected]', 'test emai', 'test'));"
bool(true)
Reading the mail
To read the emails delivered I use mutt from the command line.