How to configure AWS SES with Postfix MTA

眾所周知email spam是internet上最讓人討厭的東西,所以很多雲端服務包括aws都會有一個默認的限制,從instance不能發送郵件出去,需要提交工單到客服那邊人工開通。

但是有時候我們只是需要測試一下application的email發送功能怎麼辦呢,難道去叫客服開通嗎?

這個時候我們可以使用postfix來把aws的ses service當作email relay就可以了,沒有申請到正式服務之前的ses會運行在sandbox,有一些限制,比如只能發送email到驗證過的domain或是email address,這很簡單,我只需要驗證一下就可以,aws會發送一封email到郵箱來驗證,點他。至於domain的部分,則是需要增加三條cname記錄,等上一兩個小時就可以。

至於發送數量和頻率,則限制在每二十四小時200封email,每秒鐘一封。

當email address和domain的驗證都完成,這個時候需要進行smtp 的配置,至少要有個username和password吧,從webconsole來配置同樣很簡單,點擊Create SMTP credentials的時候,他就會自動在IAM中創建最小權限的用戶以及access key作為smtp的password。

在aws創建好ses資源,接下來就是在instance上配置postfix的部分,

以Amazon linux 2023為例,首先安裝必要的組件:

dnf install postfix cyrus-sasl-plain -y

然後為postfix 增加一點點配置,請注意改成你自己的domain:

postconf -e "relayhost = [email-smtp.ap-northeast-3.amazonaws.com]:587" \
"smtp_sasl_auth_enable = yes" \
"smtp_sasl_security_options = noanonymous" \
"smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd" \
"smtp_use_tls = yes" \
"smtp_tls_security_level = encrypt" \
"smtp_tls_note_starttls_offer = yes" \
"myhostname = aws.bbken.org" \
"mydomain = bbken.org" \
"smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt"

vi /etc/postfix/sasl_passwd

[email-smtp.ap-northeast-3.amazonaws.com]:587 AKIJJ6XRR6XR6XR6XR54:jKKDeblcjKKDeblcPuPGYKDeblcPuPGYuPGYcVqnE3y

chmod 600 /etc/postfix/sasl_passwd

postmap -v hash:/etc/postfix/sasl_passwd

systemctl enable postfix

systemctl start postfix

最後我們可以進行一點點測試:

sendmail -vv [email protected]

或者

echo 'This is a test mail from aws ses' | mail -s 'Your Amazon SES account is in the sandbox in Asia Pacific (Osaka)' [email protected]

如果需要發送email到別的domain,也就是任意郵箱,那麼需要在ses 服務裡面Request production access。

參考文件:https://www.cyberciti.biz/faq/how-to-configure-aws-ses-with-postfix-mta/