Setting up a linux server to forward all incoming mails

Typ­i­cal thing when hav­ing a new site up: you want to get whatever@yourdomain.com to end up in you nor­mal inbox, say Gmail.

Here are the steps!
Note: If you are logged in as root, you do not need to use “sudo” in front of each com­mand.

Preparations

It’s rec­om­mend­ed to have a user account to receive the mail oth­er than root.
So either choose a user you have cre­at­ed already or cre­ate a new one:

sudo adduser [username]

Does your provider require you to use an SMTP relay? (check the doc­u­men­ta­tion, or ask). You need to know this to con­tin­ue.

Install postfix

Post­fix is a soft­ware pack­age, a MTA, it han­dles incom­ing mail and for­ward­ing.

sudo apt-get install postfix

And answer all con­fig­u­ra­tion ques­tions as below:
Server con­fig­u­ra­tion type: If you use an SMTP relay, choose “Inter­net with smarthost”, oth­er­wise “Inter­net”
Sys­tem mail name: [yourdomain.com] (no sub­do­main)
SMTP relay host: [smtp.yourprivider.com] (only show­ing if you chose “Inter­net with smarthost”)
Root and post­mas­ter mail recip­i­ent: The created/chosen before (not root)
Oth­er des­ti­na­tions to accept mail for: keep the sug­gest­ed defaults and add in front [yourdomain.com] and a space
Force syn­chro­nous updates on mail queue: no
Local net­works: keep sug­gest­ed
Use proc­mail for local deliv­ery?: yes
Mail­box size lim­it: 0
Local address exten­sion char­ac­ter: +
Inter­net pro­to­cols to use: all

Manual configuration

All edits with sudo if you not logged in as root.
Edit /etc/postfix/main.cf add fol­low­ing 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]

Final­ly, after edit­ing this file call

sudo postmap /etc/postfix/virtual

so that postmap actu­al­ly applies your changes.

Notes

If you for­ward to Gmail, do not use the same address to send test mails that the mails will be for­ward­ed to — Gmail will silent­ly ignore/delete mail where the sender and recip­i­ent are the same and you will think the for­ward­ing does not work.

Leave a Reply

Your email address will not be published. Required fields are marked *