====== CentOS7 安裝 postfix 透過 gmail 寄信 ====== 因為很多系統訊息需要透過 Mail 寄出, 目前透過 GMail 寄出信件是最容易不被當成垃圾信件 * 安裝環境: CentOS Linux release 7.7.1908 (Core) ===== 安裝相關套件 ===== * yum install postfix cyrus-sasl{,-plain} ===== 編輯相關設定檔 ===== * 編輯 /etc/postfix/main.cf : #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 : * 編輯 /etc/postfix/sasl_passwd [smtp.gmail.com]:587 username:password * 如果 Google 的帳號有設定二階段認證, 密碼的部份就必須到 Google 帳號產生應用程式密碼 * 參考 - https://support.google.com/mail/?p=InvalidSecondFactor * 產生 sasl_passwd.db postmap /etc/postfix/sasl_passwd * 讓 postfix 新設定生效 systemctl start postfix systemctl enable postfix * 透過 /var/log/maillog 可以看到信件寄信紀錄, 類似以下訊息 : Dec 10 13:43:39 ct-wiki postfix/pickup[4911]: 16FFD46459: uid=48 from= Dec 10 13:43:39 ct-wiki postfix/cleanup[4958]: 16FFD46459: message-id=<20191210054339.16FFD46459@ct-wiki.localdomain> Dec 10 13:43:39 ct-wiki postfix/qmgr[4912]: 16FFD46459: from=, size=2173, nrcpt=1 (queue active) Dec 10 13:43:41 ct-wiki postfix/smtp[4960]: 16FFD46459: to=, 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=, size=2173, nrcpt=1 (queue active) Dec 10 13:45:24 ct-wiki postfix/smtp[4961]: B37DF462D8: to=, 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 ===== 參考網址 ===== * [[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}}