Postfix Mail

How to send mails in POSTFIX through external SMTP

Postfix Mail
Postfix Mail

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.

nano /etc/postfix/main.cf

Configuration


relayhost = smtp.yourmailserver.com
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options =

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:

nano /etc/postfix/sasl_passwd

smtp.yourmailserver.com username:password

Now we must convert /etc/postfix/sasl_passwd into a format that Postfix can read:

postmap /etc/postfix/sasl_passwd

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.

nano /etc/postfix/main.cf

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:

nano /etc/postfix/generic

Here is an example of my mapping address:

his@localdomain.local      hisaccount@example.com
her@localdomain.local     heraccount@example.com

Now we must convert /etc/postfix/generic into a format that Postfix can read:

postmap /etc/postfix/generic

All that is left to do is restart Postfix:

/etc/init.d/postfix restart

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