Linux server forwarding mails

Posted 9/25/2014

Setting up a linux server to forward all incoming mails

Typical thing when having a new site up: you want to get whatever@yourdomain.com to end up in you normal inbox, say Gmail. Usually, websites selling domains offer this feature as a service. But if you find yourself in the situation to go all manual, here are the steps.

Note: This guide is written for Debian-based distros. If you are logged in as root, you do not need to use “sudo” in front of each command.

Preparations

It’s recommended to have a user account to receive the mail other than root. So either choose a user you have created already or create a new one:

sudo adduser [username]

Does your provider require you to use an SMTP relay? (check the documentation, or ask). You need to know this to continue.

Install postfix

Postfix is a software package, a MTA, it handles incoming mail and forwarding.

sudo apt-get install postfix

And answer all configuration questions as below, replace […] with your domain:

Server configuration type: If you use an SMTP relay, choose “Internet with smarthost”, otherwise “Internet”.

QuestionConfiguration
System mail name[yourdomain.com] (no subdomains)
SMTP relay host[smtp.yourprivider.com] (only showing if you chose “Internet with
Root and postmaster mail recipientThe name of the user created/chosen before (not root)
Other destinations to accept mail forkeep the suggested defaults and add in front
Force synchronous updates on mail queueno
Local networkskeep suggested
Use procmail for local delivery?yes
Mailbox size limit0
Local address extension character+
Internet protocols to useall

Manual configuration

Edit /etc/postfix/main.cf (e.g. with sudo nano ...) add following two lines to the end:

virtual_alias_domains = [yourdomain.com]
virtual_alias_maps = hash:/etc/postfix/virtual

Create/edit /etc/postfix/virtual and add:

@[yourdomain.com]    [your.email.address@somewhere.com]

Finally, after editing this file run:

sudo postmap /etc/postfix/virtual

so that postmap actually applies your changes.

Notes

If you forward to Gmail, do not use the same address to send test mails that the mails will be forwarded to—Gmail will silently ignore/delete mail where the sender and recipient are the same and you will think the forwarding does not work. How do I know? Don’t ask. 😅

Tags