How to configure postfix to send email via Gmail

此博文为copy版本,可能不及时更新,要想查看最新版本,请访问:

https://it.smallstrong.org/2019/05/how-to-configure-postfix-to-relay-email.html?view=classic

1. Why relay via Gmail?

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.

 

2. Gmail setup

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!

3. Postfix installation and configuration

3.1 Install postfix

We use CentOS 7 in our test. To install required packages, do the below.

 

yum install postfix mailx cyrus-sasl cyrus-sasl-plain

3.2 create a password file

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

 

3.3 Configure postfix as a SASL client

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

 

4 Send a test email

run:

 

date | mail -s 'Test relay via Gmail' recipient@domain.com

5. Further thought

  • how to keep the "from address" from being changed by Gmail relay server
  • how to receive emails

你可能感兴趣的:(Linux相关)