Google Cloud 與 Mailgun 合作,提供含有程式輔助 API、記錄保留、電子郵件個人化、分析和電子郵件驗證功能的電子郵件服務。
以下操作說明將 Mailgun 設為 Postfix 的電子郵件轉發服務。
事前準備
前往 Google Cloud Marketplace 註冊並建立新的 Mailgun 帳戶。
取得您的憑證。操作說明中的操作需要使用您的 Mailgun SMTP 使用者名稱、密碼和主機名稱。您可以在 Mailgun 控制台的「Domains」區段下方,取得您的使用者名稱和密碼。
視 Mailgun 中網域的設定方式而定,SMTP 主機名稱可能是
smtp.mailgun.org
或smtp.eu.mailgun.org
。設定防火牆規則,允許 TCP 通訊端
2525
傳送外送流量。
使用 Postfix 將 Mailgun 設為郵件轉發
將 Mailgun 設為郵件轉發,即可讓 Postfix 郵件傳輸代理程式轉寄要進行遠端遞送的電子郵件。
使用安全殼層 (SSH) 連線至您的執行個體。
gcloud compute ssh [INSTANCE_NAME]
其中
[INSTANCE_NAME]
是您要從其中傳送電子郵件的 VM 執行個體的名稱。成為超級使用者並設定安全的 umask。
user@test-instance:~$ sudo su -
root@test-instance:~# umask 077
安裝 Postfix Mail Transport Agent (Postfix 郵件傳輸代理程式)。
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
系統顯示提示時,請選取
Local Only
設定,並接受網域名稱的預設選擇。修改 Postfix 設定選項。Postfix 設定選項可於
main.cf
檔案中設定。請以文字編輯器開啟該檔案,可自由選擇要使用的文字編輯器。root@test-instance:~# vi /etc/postfix/main.cf
如果設定選項已存在,請加註下列幾行。
# default_transport = error # relay_transport = error
在檔案結尾處加入下列一行,以新增 Mailgun SMTP 服務。
relayhost = [smtp.mailgun.org]:2525
如要針對這些要求強制執行安全資料傳輸層 (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
儲存變更並關閉檔案。
產生 SASL 密碼對應。
建立準備供標準輸入使用的新密碼檔案。
root@test-instance:~# cat > /etc/postfix/sasl_passwd << EOF
系統提示時,請輸入服務的詳細資料,並將
YOUR_SMTP_LOGIN
和YOUR_SMTP_PASSWORD
替換為您的憑證。如要瞭解如何查看或變更個別網域的憑證,請參閱 Mailgun 說明。> [smtp.mailgun.org]:2525 YOUR_SMTP_LOGIN:YOUR_SMTP_PASSWORD
輸入分隔符號
EOF
,關閉和儲存檔案。> EOF
使用
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
接著,移除內含憑證的檔案,因為現在已不需要該檔案。
root@test-instance:~# rm /etc/postfix/sasl_passwd
設定
.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
最後請重新載入設定,以載入修改後的參數。
Debian
root@test-wheezy:~# /etc/init.d/postfix restart
CentOS
[root@test-centos ~]# postfix reload
測試您的設定。安裝
mailx
或mailutils
套件並測試您的設定。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 的參考架構、圖表和最佳做法。歡迎瀏覽我們的雲端架構中心。