Postfix mail forwarding
Postfix is a commonly used MTA (Mail Transfer Agent) program that can receive, deliver or route emails. So if you have your own domain and a server then using postfix you can setup emails on the domain like [email protected].
Setting up a full featured mail server with postfix is a lengthy process requiring quite some effort. Furthermore, using your own mail server you do miss out the convenience and features of the popular free email services like gmail or hotmail. One way is to use google's paid service to integrate gmail with the domain email.
Alternatively, setup your webserver to forward all mails coming on your domain email, to your other address like gmail. Doing so would allow you to accept mails on your personal domain emails, making it look professional and at the same time use gmail service to access the same and enjoy the convenience of gmail too.
mail for [email protected] ==> [ site.com Postfix server ] ==> forwarded to [email protected]
So this post shows how to setup mail forwarding on a Debian or Ubuntu server using the postfix program. For other distros like CentOS and Fedora too, the steps are nearly the same.
1. Install Postfix
Installation is super quick. Postfix is there in the default repository of Ubuntu or Debian, so just use apt-get to install.
$ sudo apt-get install postfix
During installation it would ask a couple of questions about how to configure the mail server. Fill in the required information and proceed. Once the installation is complete, the mail server daemon would be started.
$ sudo service postfix status * postfix is running
You can further check that postfix is running a server on port 25 with the netstat command
$ sudo netstat -ltnp | grep 25 tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1926/master tcp6 0 0 ::1:25 :::* LISTEN 1926/master
The last column shows the pid of the processing, which is postfix here.
2. Verify DNS settings of domain
Before proceeding to configuring postfix to forward mails on behalf of your domain, its necessary to ensure that the MX records for your domain are pointing to the right server.
This can be done quickly with the dig command. Run it from your local machine.
$ dig binarytides.com mx ; <<>> DiG 9.9.3-rpz2+rl.13214.22-P2-Ubuntu-1:9.9.3.dfsg.P2-4ubuntu1.1 <<>> binarytides.com mx ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 7230 ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;binarytides.com. IN MX ;; ANSWER SECTION: binarytides.com. 85100 IN MX 10 mail.binarytides.com. ;; Query time: 74 msec ;; SERVER: 127.0.1.1#53(127.0.1.1) ;; WHEN: Fri Mar 28 08:59:17 IST 2014 ;; MSG SIZE rcvd: 65
In the 'ANSWER SECTION' it can be seen that mail.binarytides.com is reported as the mail server for binarytides.com. Next, dig up the a records for the domain mail.binarytides.com and it would show the server ip to which it points.
$ dig mail.binarytides.com a ; <<>> DiG 9.9.3-rpz2+rl.13214.22-P2-Ubuntu-1:9.9.3.dfsg.P2-4ubuntu1.1 <<>> mail.binarytides.com a ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 43356 ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;mail.binarytides.com. IN A ;; ANSWER SECTION: mail.binarytides.com. 85045 IN A 126.96.36.199 ;; Query time: 30 msec ;; SERVER: 127.0.1.1#53(127.0.1.1) ;; WHEN: Fri Mar 28 09:00:19 IST 2014 ;; MSG SIZE rcvd: 65
Now the ANSWER SECTION shows the ip address of mail.binarytides.com which over here is correct. Do the same for your domain and verify that the mx records are setup correctly.
3. Configure postfix to forward mails
Now comes the final task of configuring postfix to forward mails, and it is surprisingly simple.
Find the path of the configuration directory using the postconf command
$ postconf | grep config_directory config_directory = /etc/postfix
Now we know that all the necessary configuration files lie in the above directory, so get inside it.
Edit the main.cf file
And add the following lines at the end of it
virtual_alias_domains = mydomain.com myanotherdomain.com virtual_alias_maps = hash:/etc/postfix/virtual
The first line virtual_alias_domains lists the domains, for which postfix is going to accept emails. Multiple domains are added separated by a space.
The second line virtual_alias_maps specifies the path to the file which is going to contain mappings specifying how to forward emails for the above domains.
Now open the /etc/postfix/virtual file (create one if it does not exist) and add to it the emails you want to forward along with the destination emails.
# lets forward 2 emails [email protected] [email protected] [email protected] [email protected]
The first email is the address on which postfix shall receive emails, and the second is the address where postfix would forward the emails.
The mail can be forwarded to multiple destinations
[email protected] [email protected] [email protected]
To catch and forward emails to any address for a given domain, use the following notation
# forward all emails @mydomain.com [email protected] [email protected]
After entering the forwarding rules, save the file and close it. Then run the following commands for the new settings to take effect
Update the postfix lookup table
$ postmap /etc/postfix/virtual
4. Reload postfix configuration
After making the necessary changes in the configuration files, its time to reload the configurations so that the changes can take effect. Either of the following command shall reload the configuration.
$ sudo /etc/init.d/postfix reload # or $ sudo service postfix reload
Check with the postconf command that the domain aliases and alias file have been setup properly.
$ postconf -n | grep virtual virtual_alias_domains = mydomain.com myanotherdomain.com virtual_alias_maps = hash:/etc/postfix/virtual [email protected]:~#
5. Test it now
Now that all configuration is complete, you can test the mail forwarding.
Try sending an email from somewhere outside the world, to the address on your domain, and you should see the same mail forwarded to the gmail account.
You should see the email arrive in the forwarded destination within a few seconds. For further diagnosis check postfix log files and check for details on how the mail was forwarded and whether it was successful or not.
Works but I am loosing the original From address, so i can’t tell where the original email came from so I can’t reply to it.
I’ve been farting around trying to do this for 2 days now, tried sendmail for a day, failed, and got myself banned from gmail, deleted sendmail, installed postfix and got further but gmail still wouldnt relay. Found this guide and wallah! working!
Thank you for this. Nightmare over.
I try this feature but its not working on me
When sent to another email via gmail relayhost, it does not show the name of the email from the original sender, it shows the name of my host configured in google
relayhost = [smtp.gmail.com]:587
From mail sender([email protected]) To destiny([email protected])
forwaring another mail ([email protected])
When arrive el mail to another [email protected] through forwarding
It does not show the mail of the sender [email protected]
Show the sender’s mail using to do the relayhost example [email protected]
I want to keep a copy of mail on server. How do i do it ?
Works fine just one small problem. When I send normal an email out via sendgrid to gmail white labeling is correct, but if email comes in and then forwarded out again via sendgrid to gmail the white labeling is not working. The email arrives ok and the addressing on the received email looks correct and what you would expect. But gmail adds the via message.
Sorry my mistake of course it will, because when forwarding the email address from domain is not my domain but the senders Domain. So when it is passed to my sendgrid account to be forwarded it will not validate as its not my Domain.
This is a flawed guide. You need something like SRS to reformat the sender’s address. This will break on any domain that uses SPF to authorize its sender’s address since the last server (yours) to relay the message likely won’t match the authorized domains in a SPF record.
I used this way for several years on a debian 6 with a postfix 2.2 / 2.3.
Now i am trying to replicate this behaviour on a debian 9 with the new postfix version but it does not seems to work properly since when i put mydomain.com in virtual_alias_domains my server logs a warning saying domain cannot be both in mydestination and virtual_alias_domains and i agree with this but since gapps wants [email protected] forwarded to [email protected], how can solve it?
the only big conf difference is that:
on the old server we used unix user accounts
on the new server we use dbplugin with users on mysql
what does your ‘mydestination’ setting in main.cnf look like? Do you specify domains there as well as under virtual_domain_alias?
Hey, thanks for your reply.
mydestination = mydomain.com, myhostname.localdomain, localhost.localdomain, localhost
myhostname is mail server hostname
localdomain is my internal domain
After several tries i can confirm that virtual forwarding works with or without virtual_alias_domain has been specified; we have some other virtual domains for internal forwading, such as @mydomain to @myotherdomain, with no issue at all.
Nevertheless i am not actually able to get it to work with google forward.
Thanks in advance
Thank you so much! Works like a charm
Perfect recipe! Saved time and money! Big Thanks to the Author!
What would be great to know, is how to encrypt messaging
Great guide !
Thanks for the guide! Helped me solve my problem.
What if I have mydomain.com in mydestination, and I want to forward all mails sent to [email protected] to [email protected]? Because virtual host domain cannot be same as mydestination. Please help
Is it possible to send an email FROM [email protected]?
Having an issue with virtual.db
I get an error message that contains fatal: open /etc/postfix/virtual.db: Permission denied
drwxr-xr-x 3 root root 4096 Jul 3 22:43 ./
drwxr-xr-x 98 root root 4096 Jul 3 17:22 ../
-rw-r–r– 1 root root 274 Jun 25 12:25 dynamicmaps.cf
-rw-r–r– 1 root root 1640 Jul 3 17:27 main.cf
-rw-r–r– 1 root root 6068 Jun 25 12:25 master.cf
-rw-r–r– 1 root root 20281 Feb 6 2015 postfix-files
-rw-r-xr-x 1 root root 8861 Feb 6 2015 postfix-script*
-rw-r-xr-x 1 root root 28047 Feb 6 2015 post-install*
drw-r-xr-x 2 root root 4096 Feb 6 2015 sasl/
-rw-r–r– 1 root root 281 Jul 3 22:41 virtual
-rw-r–r– 1 root root 12288 Jul 3 22:43 virtual.db
What permissions are best for virtual.db?
FYI: to update the postfix lookup table I needed to use it with sudo:
sudo postmap /etc/postfix/virtual
Hi There, Great post! Thank you :) I just had one problem. I have followed your guide exactly but my emails are not being delivered. I get the following error when trying to forward email “Diagnostic-Code: X-Postfix; User unknown in virtual alias table”. Can anyone help?
Warning! I have encountered that with a default postfix configuration this will open your server for SPAM as it allows log in via unauthenticated locations. Dear author, please add a line on smtp restrictions!
I have disabled SASL as it seems to be unneeded for forwarding only and disable the unauthenticated destinations for now as a first step.
smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
Mar 29 19:23:10 planetsofa postfix/smtpd: warning: hostname 188.8.131.52.dynamic.ttnet.com.tr does not resolve to address 184.108.40.206: Name or service not known
Mar 29 19:23:10 planetsofa postfix/smtpd: connect from unknown[220.127.116.11]
Mar 29 19:23:10 planetsofa postfix/smtpd: 5F14113E0005: client=unknown[18.104.22.168]
Mar 29 19:23:10 planetsofa postfix/cleanup: 5F14113E0005: message-id=
You are a life saver.
I was having an absolute nightmare with Google Apps I needed just one email forwarded on this domain as the old hosting company was shutting up shop, bugs and delays everywhere doing it on Google Apps, support says wait 24 hours, I DON’T HAVE 24 HOURS EMAILS ARE BLACK HOLING!!
Thanks to you, sorted (for now).
Work perfectly, but I just want to report a problem. I configured forwarding email [email protected] to [email protected]. Then I tested sending mail from [email protected] to [email protected], but when I refreshed my inbox I did not receive any mail. It seems that gmail cannot send to and receive from the same email address. Could someone explain me why? Thanks.
Having the same issue here. I guess it’s not really an issue, but I’d like to know why this happens.
same thing, glad to hear its not me.
thank you for the post, everything works (except the gmail to gmail thing)
It won’t work sending back to the same email. Its not a problem, its a feature LOL!
Hi. I forwarded all emails (@something.com) to my gmail account. I have only one question. after forwarding an email will postfix delete any local data related to the email? or it will save the same email on the server too? where is the postfix folder for saving emails? /var/mail/ is always empty for me
Yes. I dont want it to store any data in the server.
Anyone can answer the question?
Thanks for your comment.. I was trying it for hours with my own gmail.. forwarding to my own gmail.. could not get it to work…
Finally scrolled down.. tried from another email account.. also gmail.. and it worked!
And also thanks for the tutorial. Its fantastic!
How do I do this from te userspace, I know there is like a ~/.forward file that allows you to choose as a user level where you want your emails to be forwarded.
How can I forward all emails sent to an address, to another corresponding address. For example, when I receive the emails sent to “[email protected]”, I want to forward it to another email with the same username, but different domain, like “[email protected]”? I just want to change the domain of target email, but not the email name. Because I don’t want to forward to my email only, but my users’ email too.
Were you able to figure this one out? I’m looking for the same thing.
I think I have problems with DNS
$ dig domain.com mx
return > “domain.com. 3600 IN MX 10 mail.domain.com.”
$ dig domain.com a
return > “domain.com. 3600 IN A 999.999.999.999”
but it should be > “MAIL.domain.com. 3600 IN A 999.999.999.999”
then create an entry ‘A’ mail pointing to the ip
$ dig domain.com mx return:
;; ANSWER SECTION:
domain.com. 3600 IN MX 10 mail.domain.com.
;; ADDITIONAL SECTION:
mail.domain.com. 3600 IN A 999.999.999.999
Is that correct?
My emails returned with error “DNS Error: Domain name not found”
This was the best tutorial I found
Your dig query for MX records returned the subdomain of mail.domain.com. Therefore you need to set an A record for this subdomain as well, and not just for the root domain.
What is the difference between alias and virtula_file?
aliases are for local (username only, no domain) redirects.