Today it's very hard to set up an email server which can be trusted by other email servers. So even if you can configure a postfix server in a couple of minutes, you cannot send email successfully to other email servers because they don't trust you and deny emails sending from your own postfix server.
Another issue is most ISPs forbid TCP port 25 so your email server cannot send SMTP with port 25. Fortunately, 587 port which is used by Gmail is allowed.
Email relay is not a new tech but it works for the above situation. And Gmail server supports it for free! Almost all other email servers trust the Gmail server.
So now the sending process is:
local postfix server ---> Gmail server --->destination email server
The only issue is that the Gmail server will change the "from address" to the Gmail account before sending the email.
The Gmail server can be connected as a relay host only if you already have a Gmail account used to log into the Gmail server.
For security reasons, the Gmail server by default refuses to be connected by non-authorized apps. In order to allow our postfix to connect to it, we need to manually configure our Gmail account's security level.
This means another hacker's app can also log in your Gmail account if he knows your password. So the best way is to create a new Gmail account dedicated used for relay purpose. Setting up a new Gmail account is easy and free!
We use CentOS 7 in our test. To install required packages, do the below.
yum install postfix mailx cyrus-sasl cyrus-sasl-plain
Then create a file /etc/postfix/sasl_passwd to store the Gmail account name and password.
vim /etc/postfix/sasl_passwd
[smtp.gmail.com]:587 [email protected]:password
To be secure, please change this file to be only accessible by its owner.
chmod 600 /etc/postfix/sasl_passwd
Then compile this text file to binary format /etc/postfix/sasl_passwd.db
postmap /etc/postfix/sasl_passwd
Next, we will modify /etc/postfix/main.cf to have the below.
vim /etc/postfix/main.cf
relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_security_options =
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
Then restart postfix.
systemctl restart postfix
run:
date | mail -s 'Test relay via Gmail' recipient@domain.com