続 カッコの付け方

AWSを始めとしたクラウドコンピューティング全般と、唯一神emacsにおける()の付け方についてだらだら書きます

GCEのVMインスタンスからメール送信するなら、SendGrid必須

stackoverflowでもこの件について触れられていますが、GCEでは、インスタンスからのOutbound:Port25は全面シャットアウトです。そのため、メール送信にはSendGridが必要となります。

Google Compute Engine (GCE) email delivery solution? - Stack Overflow

SendGridアカウント作成

GCPとSendGridの提携により、月 25,000通は無料で送れるようです。その気になればAWS SESも使えるはずですが、無料分があるので、、

VMインスタンスの設定

あとはGoogleの公式ドキュメントの日本語訳です(^^)ちなみに環境はCentOS

Sending Email from an Instance - Google Compute Engine — Google Cloud Platform

1) sshインスタンスに入ります。

$ gcloud compute ssh INSTANCE

2) rootになって、 umask 077を指定

[user@test-centos ~]$ sudo su -  
[root@test-centos ~]# umask 077

3) /etc/postfix/sasl_passwd ファイルを作り、下記の書式でSendGridのユーザー名:パスワードを入れてください。 改行無しでね。

[root@test-centos ~]# cat > /etc/postfix/sasl_passwd << EOF
[smtp.sendgrid.net]:2525 YOUR_SENDGRID_USERNAME:YOUR_SENDGRID_PASSWORD
EOF

4) postmapコマンドで.db作成。

[root@test-centos ~]# postmap /etc/postfix/sasl_passwd
[root@test-centos ~]# 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

5) /etc/postfix/main.cfに追記

[root@test-centos ~]# cat >> /etc/postfix/main.cf << EOF
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = encrypt
header_size_limit = 4096000
relayhost = [smtp.sendgrid.net]:2525
EOF

6) 認証ライブラリが最新かどうか、確認してね

[root@test-centos ~]# yum install cyrus-sasl-plain

7) postfix を reloadしてね。(訳者注 postfix は restart > reloadとは限らないところがあったはずなので、明示的に reloadが必要なはず。)

[root@test-centos ~]# postfix reload
postfix/postfix-script: refreshing the Postfix mail system

8) プレーンテキストのsasl_passwdはもう不要だから、消しといて。

動作確認

mailコマンドで飛ばしてみましょう。届いたメールの Return-Path: は xxxxxxx@sendgrid.meとなってます。

もはや、Outbound:25のブロックが主流

スパムに使われる対策ですよねー。Asure, SoftLayler, GCEともにメール出すならSendGrid使ってです。むしろ素の状態で1通でもメール送信できるAWSのほうが稀少なぐらい。