如何配置Postfix以在Ubuntu 16.04 / 17.10上使用Gmail SMTP

当我在我的终端尝试此代码时,使用此代码安装postfix邮件服务器sudo apt install mailutils

 echo "This is the body of the email" | mail -s "This is the subject line" marksman283@gmail.com 

它运行正常但我想设置我的Gmail帐户如何设置?

注意我的Ubuntu版本:18.1

1.configure Postfix

编辑Postfix配置文件。

 sudo nano /etc/postfix/main.cf 

找到以下行relayhost =从文件底部向上约6行并删除它。

将以下内容添加到文件末尾。

 relayhost = [smtp.gmail.com]:587 smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtp_sasl_security_options = noanonymous smtp_tls_CAfile = /etc/postfix/cacert.pem smtp_use_tls = yes 

保存文件并退出。 (按CTRL + X,按Y然后按ENTER键)

2.创建密码和数据库文件

创建将存储我们的凭据的sasl_passwd文件。

 sudo nano /etc/postfix/sasl_passwd 

插入以下内容:

 [smtp.gmail.com]:587 username@gmail.com:password 

保存文件并退出。 (按CTRL + X,按Y然后按ENTER键)

使用postmap命令为Postfix创建哈希数据库文件。

 sudo postmap /etc/postfix/sasl_passwd 

现在应该在/ etc / postfix /目录中有一个名为sasl_passwd.db的文件。

为了增加安全性,我们只允许root用户读取和写入sasl_passwd和sasl_passwd.db

 sudo chown root:root /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db sudo chmod 0600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db 

3.发送测试邮件

我们现在发送测试电子邮件。 请务必使用您自己的电子邮件地址替换test@example.com。

 echo "Test Email message body" | mail -s "Email test subject" test@example.com