Postfixの送信メールに、Gmailを使う

Linux
スポンサーリンク

独自ドメインを取っていないサーバーで、ログやアラートを外部のメールに飛ばしたいなあ。
そうだ、Gmailの送信メールサーバーを借りよう、ということで、設定してみました。
環境は、CentOS7、Postfix2.10、です。

SASL authentication failed; cannot authenticate to server smtp.gmail.com
SASL authentication failed; server smtp.gmail.com
Network is unreachable
なんてエラーが表示されましたが、何とか解決し送信することができました。

送信用のアカウント取得

普段使っているGmailアカウントでも良いのですが、混乱を避けるため、送信用のGmailアカウントを取得しました。

Gmailアカウントへの安全性の低いアプリの許可

外部のアプリ(今回はPostfix)からの接続を許可する設定を行います。安全性の低いアプリ設定

cyrus-saslのインストール

cyrus-saslがないと認証がうまくいきません。yumでインストールします。

[sourcecode language=”bash”]
yum -y install cyrus-sasl
[/sourcecode]

Postfixの設定

設定ファイルを編集します。

[sourcecode language=”bash”]
vi /etc/postfix/main.cf
[/sourcecode]
[sourcecode language=”bash”]
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/authinfo
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
smtp_sasl_mechanism_filter = plain
smtp_use_tls = yes
[/sourcecode]

Gmailの認証ファイル作成

Gmailへのアカウント認証は、認証ファイルを使用します。

[sourcecode language=”bash”]
vi /etc/postfix/authinfo
[/sourcecode]
[sourcecode language=”bash”]
[smtp.gmail.com]:587 {アカウント名}@gmail.com:{パスワード}
[/sourcecode]

認証ファイルをハッシュ化

下記のコマンドで、ハッシュ化を行います。

[sourcecode language=”bash”]
postmap /etc/postfix/authinfo
[/sourcecode]

テストメール

テストメールを送信してみましょう。

[sourcecode language=”bash”]
postmap /etc/postfix/authinfo
[/sourcecode]

コメント

タイトルとURLをコピーしました