Saturday, February 17, 2007

Configuring Postfix to forward emails for multiple domains to local mailbox

A friend was recently looking to configure postfix, so that he could send emails from his laptop to several domains and have all of them show up in his local mailbox. I'm posting the simple configuration for postfix that helps make this happen.

  1. Check your postfix configuration. If you don't already have postfix, you can get it here. The instructions below all assume you have a Unix OS and shell. I did the below on a Mac OS X as well as a Sun OS host. I am sure it almost identical on most Unix environments.
    • Check that you have the postfix executable installed. (Usually in /usr/sbin/postfix)
    • Check that the configuration directory and files exist. (/etc/postfix/main.cf)
    • Ensure you have the access to edit the configuration files and create files in the postfix configuration directory. You will also need to restart postfix.
2. Edit the /etc/postfix/main.cf file. Add these lines at the end: (double check to make sure your spellings are correct. :)

# Testing virtual host domains
virtual_alias_domains = testdomain1.com testdomain2.com
virtual_alias_maps = hash:/etc/postfix/virtual


3. Edit /etc/postfix/virtual, and and these lines: (Replace recipient by any Unix user account you want to receive emails on your machine).

@testdomain1.com recipient
@testdomain2.com recipient


4. In the directory /etc/postfix, run the following command. This creates the hash database file needed by postfix for virtual alias table.

$> postmap hash:virtual


5. This above command would have created the file virtual.db in this directory. Verify.

6. Restart postfix. In case postfix is not running, you use "postfix start" to get it going.



$> sudo postfix reload
postfix/postfix-script: refreshing the Postfix mail system
$>


7. Test: (the user running the second command is the user whose account emails are being forwarded to, as per the settings in step 2 above).



$> mail -s "test mail" anyuser@testdomain1.com < /dev/null
Null message body; hope that's ok
$> mail
Mail version 8.1 6/6/93. Type ? for help.
"/var/mail/vikasgupta": 1 message 1 unread
>U 1 vikasgupta@localhost Thu Feb 15 11:33 16/505 "test mail"
$>


Postfix comes with plenty of documentation on how to configure and use it. For more details on the above configuration, look at the VIRTUAL_README in the postfix distribution. The utility postconf is useful to review the configuration once you've reloaded it into postfix.

No comments: