This short guide shows how you can set up Postfix to relay emails through another mailserver. This can be useful if you run a Postfix mailserver in your local network and have a dynamic IP address because most dynamic IP addresses are blacklisted today. By relaying your emails through another mailserver that is hosted on a static IP address in a data center (e.g. your ISP’s mailserver) you can prevent your emails from being categorized as spam.
First step, you will need to make changes in your configuration file (in standard postfix configuration it would be /etc/postfix/main.cf). You need to add at the end of configuration file following lines:
I’m going to use Nano editor to edit my configuration file.
relayhost = smtp.yourmailserver.com
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
Step two, generate password file. Postfix have to know your account login and password as most SMTP servers would not allow to rely emails from unauthorized connections. You will need to make a password file “/etc/postfix/sasl_passwd” with similar content:
Now we must convert /etc/postfix/sasl_passwd into a format that Postfix can read:
This will create the file /etc/postfix/sasl_passwd.db.
You may test it by typing:
postmap -q smtp.yourmailserver.com /etc/postfix/sasl_passwd
If you will get an output like “username:password” it means that password file has been generated correctly.
You can remove “/etc/postfix/sasl_passwd“. POSTFIX will not need it anymore.
rm -f /etc/postfix/sasl_passwd
Optional* Mapping, Postfix 2.2 uses the generic(5) address mapping to replace local fantasy email addresses by valid Internet addresses. This mapping happens ONLY when mail leaves the machine; not when you send mail between users on the same machine.
Add this line to your config file:
smtp_generic_maps = hash:/etc/postfix/generic
Now you need to add your mapping in /etc/postfix/generic:
Here is an example of my mapping address:
Now we must convert /etc/postfix/generic into a format that Postfix can read:
All that is left to do is restart Postfix:
If you are using Plesk, you can restart the mail service using the following command:
/usr/local/psa/admin/bin/mailmng --restart-service --mail-component=mta