使用 Mailgun 傳送電子郵件

Google Cloud 與 Mailgun 合作提供電子郵件服務,包括程式輔助 API、記錄保留、電子郵件個人化、分析和電子郵件驗證。

以下說明如何使用 Postfix 將 Mailgun 設為電子郵件轉發服務。

事前準備

  1. 前往 Google Cloud Marketplace 註冊並建立新的 Mailgun 帳戶。

  2. 取得您的憑證。操作說明中的操作需要使用您的 Mailgun SMTP 使用者名稱、密碼和主機名稱。您可以在 Mailgun 控制台的「Domains」區段下方,取得您的使用者名稱和密碼。

    視網域在 Mailgun 中的設定方式而定,SMTP 主機名稱為 smtp.mailgun.orgsmtp.eu.mailgun.org

  3. 設定防火牆規則,允許 TCP 通訊埠 2525 傳送外送流量。

使用 Postfix 將 Mailgun 設為郵件轉發

將 Mailgun 設為郵件轉發,即可讓 Postfix 郵件傳輸代理程式轉寄要進行遠端遞送的電子郵件。

  1. 使用安全殼層 (SSH) 連線至您的執行個體。

    gcloud compute ssh [INSTANCE_NAME]
    

    其中 [INSTANCE_NAME] 是您要從其中傳送電子郵件的 VM 執行個體的名稱。

  2. 成為超級使用者並設定安全的 umask。

    user@test-instance:~$ sudo su -
    
    root@test-instance:~# umask 077
    
  3. 安裝 Postfix Mail Transport Agent。

    Debian

    root@test-instance:~# apt update && apt -y install postfix libsasl2-modules
    

    CentOS

    root@test-instance:~# yum install postfix cyrus-sasl-plain cyrus-sasl-md5 -y
    

  4. 系統顯示提示時,請選取 Local Only 設定,並接受網域名稱的預設選擇。

  5. 修改 Postfix 設定選項。Postfix 設定選項可於 main.cf 檔案中設定。請以文字編輯器開啟該檔案,可自由選擇要使用的文字編輯器。

    root@test-instance:~# vi /etc/postfix/main.cf
    
  6. 如果設定選項已存在,請加註下列幾行。

    # default_transport = error
    # relay_transport = error
    
  7. 在檔案結尾處加入下列一行,以新增 Mailgun SMTP 服務。

    relayhost = [smtp.mailgun.org]:2525
    
  8. 如要針對這些要求強制執行安全資料傳輸層 (SSL)/傳輸層安全標準 (TLS) 支援,並設定 SMTP 驗證,請在檔案結尾處加入下列幾行。簡單存取及安全性階層 (SASL) 模組可處理 Postfix 設定中的驗證作業。

    smtp_tls_security_level = encrypt
    smtp_sasl_auth_enable = yes
    smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
    smtp_sasl_security_options = noanonymous
    
  9. 儲存變更並關閉檔案。

  10. 產生 SASL 密碼對應。

    1. 建立準備供標準輸入使用的新密碼檔案。

      root@test-instance:~# cat > /etc/postfix/sasl_passwd << EOF
      
    2. 系統提示時,請輸入服務的詳細資料,並將 YOUR_SMTP_LOGINYOUR_SMTP_PASSWORD 替換為您的憑證。如要查看或變更網域憑證,請參閱 Mailgun 說明

      > [smtp.mailgun.org]:2525 YOUR_SMTP_LOGIN:YOUR_SMTP_PASSWORD
      
    3. 輸入分隔符號 EOF,關閉和儲存檔案。

      > EOF
      
  11. 使用 postmap 公用程式產生 .db 檔案。

    root@test-instance:~# postmap /etc/postfix/sasl_passwd
    
    root@test-instance:~# ls -l /etc/postfix/sasl_passwd*
    
    -rw------- 1 root root    68 Jun  1 10:50 /etc/postfix/sasl_passwd
    -rw------- 1 root root 12288 Jun  1 10:51 /etc/postfix/sasl_passwd.db
    
  12. 接著,移除內含憑證的檔案,因為現在已不需要該檔案。

    root@test-instance:~# rm /etc/postfix/sasl_passwd
    
  13. 設定 .db 檔案的權限。

    root@test-instance:~# chmod 600 /etc/postfix/sasl_passwd.db
    
    root@test-instance:~# ls -la /etc/postfix/sasl_passwd.db
    
    -rw------- 1 root root 12288 Aug 31 18:51 /etc/postfix/sasl_passwd.db
    
  14. 最後請重新載入設定,以載入修改後的參數。

    Debian

    root@test-wheezy:~# /etc/init.d/postfix restart
    

    CentOS

    [root@test-centos ~]# postfix reload
    

  15. 測試您的設定。安裝 mailxmailutils 套件並測試您的設定。

    Debian

    root@test-wheezy:~# apt -y install mailutils
    

    CentOS

    [root@test-centos ~]# yum install mailx -y
    

    傳送測試電子郵件

    root@test-instance:~# echo 'Test passed.' | mail -s 'Test-Email' EMAIL@EXAMPLE.COM
    

    請在系統記錄中尋找內含 status 的狀態行,以及成功的伺服器回應碼 (250)

    Debian

    root@test-wheezy:~# tail -n 5 /var/log/syslog
    

    CentOS

    [root@test-centos ~]# tail -n 5 /var/log/maillog
    

如需其他主題的詳細範例與相關資訊 (包括如何追蹤與轉送訊息等主題),請參閱 Mailgun 說明文件

探索 Google Cloud 的參考架構、圖表和最佳做法。 歡迎瀏覽我們的雲端架構中心