使用者工具

網站工具


tech:postfix

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
  • 產生 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=<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
    

參考網址

tech/postfix.txt · 上一次變更: 2019/12/10 14:02 由 jonathan_tsai