差異處

這裏顯示兩個版本的差異處。

連向這個比對檢視

下次修改
前次修改
tech:postfix [2019/12/10 13:59] – 建立 jonathan_tsaitech:postfix [2021/01/16 14:40] (目前版本) – 更換 plugin jonathan
行 1: 行 1:
 +====== CentOS7 安裝 postfix 透過 gmail 寄信 ======
 +
 +因為很多系統訊息需要透過 Mail 寄出, 目前透過 GMail 寄出信件是最容易不被當成垃圾信件
 +
 +  * 安裝環境: CentOS Linux release 7.7.1908 (Core)
 +
 +===== 安裝相關套件 =====
 +  * <code sh>
 +yum install postfix cyrus-sasl{,-plain}
 +</code>
 +
 +===== 編輯相關設定檔 =====
 +  * 編輯 /etc/postfix/main.cf <file>
 +:
 +#relayhost = [an.ip.add.ress]
 +relayhost = [smtp.gmail.com]:587
 +
 +smtp_use_tls=yes
 +# use sasl when authenticating to foreign SMTP servers
 +smtp_sasl_auth_enable = yes
 +# path to password map file
 +smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
 +# list of CAs to trust when verifying server certificate
 +smtp_tls_CAfile = /etc/pki/ca-trust/extracted/pem/email-ca-bundle.pem
 +# eliminates default security options which are imcompatible with gmail
 +smtp_sasl_security_options = noanonymous
 +smtp_always_send_ehlo = yes
 +
 +
 +# REJECTING UNKNOWN RELAY USERS
 +:
 +</file>
 +  * 編輯 /etc/postfix/sasl_passwd <file>
 +[smtp.gmail.com]:587  username:password
 +</file>
 +<note warning>
 +  * 如果 Google 的帳號有設定二階段認證, 密碼的部份就必須到 Google 帳號產生應用程式密碼
 +  * 參考 - https://support.google.com/mail/?p=InvalidSecondFactor
 +</note>
 +  * 產生 sasl_passwd.db <code sh>
 +postmap /etc/postfix/sasl_passwd
 +</code>
 +  * 讓 postfix 新設定生效 <code sh>
 +systemctl start postfix
 +systemctl enable postfix
 +</code>
 +  * 透過 /var/log/maillog 可以看到信件寄信紀錄, 類似以下訊息 <cli>
 +:
 +Dec 10 13:43:39 ct-wiki postfix/pickup[4911]: 16FFD46459: uid=48 from=<apache>
 +Dec 10 13:43:39 ct-wiki postfix/cleanup[4958]: 16FFD46459: message-id=<[email protected]>
 +Dec 10 13:43:39 ct-wiki postfix/qmgr[4912]: 16FFD46459: from=<[email protected]>, size=2173, nrcpt=1 (queue active)
 +Dec 10 13:43:41 ct-wiki postfix/smtp[4960]: 16FFD46459: to=<[email protected]>, relay=smtp.gmail.com[64.233.188.109]:587, delay=2.1, delays=0.03/0.01/0.98/1, dsn=2.0.0, status=sent (250 2.0.0 OK  1575956621 x4sm1408923pff.143 - gsmtp)
 +Dec 10 13:43:41 ct-wiki postfix/qmgr[4912]: 16FFD46459: removed
 +Dec 10 13:45:23 ct-wiki postfix/qmgr[4912]: B37DF462D8: from=<[email protected]>, size=2173, nrcpt=1 (queue active)
 +Dec 10 13:45:24 ct-wiki postfix/smtp[4961]: B37DF462D8: to=<[email protected]>, relay=smtp.gmail.com[2404:6800:4008:c06::6d]:587, delay=546, delays=545/0.01/0.97/0.69, dsn=2.0.0, status=sent (250 2.0.0 OK  1575956724 200sm1420912pfz.121 - gsmtp)
 +Dec 10 13:45:24 ct-wiki postfix/qmgr[4912]: B37DF462D8: removed
 +</cli>
 +
 +
 +===== 參考網址 =====
 +  * [[https://www.ichiayi.com/wiki/tech/pvetips#pve_%E4%B8%BB%E6%A9%9F%E8%A8%AD%E5%AE%9A_postfix_%E7%94%B1_gmail_%E5%AF%84%E7%99%BC%E4%BF%A1%E4%BB%B6%E6%96%B9%E5%BC%8F|PVE 主機設定 Postfix 由 GMail 寄發信件方式]]
 +  * https://rricketts.com/postfix-sasl-authentication-failure-no-worthy-mechs-found/
 +
 +{{tag>mail}}