そういうのがいいブログ

SIerで働く30代サラリーマンSEがインフラエンジニアに憧れてLinux・クラウド・AWSの勉強をするブログ

PostfixからGmailにリレーホスト設定してメール送信するサーバーを構築

※[PR]当ブログの記事の中にはプロモーションが含まれています。

PostfixからGmailへリレーホスト設定してメール送信するサーバーを構築する方法を知りたい。
・具体的な手順を分かりやすく教えてほしい。

こういった疑問に答えます。

本記事の内容

  1. PostfixからGmailにリレーホスト設定してメール送信するサーバーを構築

サトナカ (@souiunogaii)

この記事を書いている私は、某SIerに勤務しながら、
WEB系エンジニア・インフラエンジニアに憧れて、プログラミングおよびインフラ技術の勉強をしています。

こういった私が、解説していきます。

私が実機で試したコマンドや画像を載せて書いています。
記事の信頼性担保に繋がると思います。

PostfixからGmailにリレーホスト設定してメール送信するサーバーを構築

手順① Postfixのmain.cfを編集

vi /etc/postfix/main.cf
    321 # The relayhost parameter specifies the default host to send mail to
    322 # when no entry is matched in the optional transport(5) table. When
    323 # no relayhost is given, mail is routed directly to the destination.
    324 #
    325 # On an intranet, specify the organizational domain name. If your
    326 # internal DNS uses no MX records, specify the name of the intranet
    327 # gateway host instead.
    328 #
    329 # In the case of SMTP, specify a domain, host, host:port, [host]:port,
    330 # [address] or [address]:port; the form [host] turns off MX lookups.
    331 #
    332 # If you're connected via UUCP, see also the default_transport parameter.
    333 #
    334 #relayhost = $mydomain
    335 #relayhost = [gateway.my.domain]
    336 #relayhost = [mailserver.isp.tld]
    337 #relayhost = uucphost
    338 #relayhost = [an.ip.add.ress]
    339 relayhost = [smtp.gmail.com]:587    ←★★変更
    753 smtp_sasl_auth_enable = yes    ←★★追加
    754 smtp_sasl_password_maps = hash:/etc/postfix/smtp_pass    ←★★追加
    755 smtp_sasl_security_options = noanonymous    ←★★追加
    756 smtp_sasl_tls_security_options = noanonymous    ←★★追加
    757 smtp_sasl_mechanism_filter = plain    ←★★追加

手順② smtp_passの作成とハッシュ化

ここで設定するパスワードは、Gmailのパスワードではなく、Googleのアプリパスワードです。
vi /etc/postfix/smtp_pass
[smtp.gmail.com]:587 ★★★ユーザー名@gmail.com:●●●●パスワード
postmap /etc/postfix/smtp_pass

手順③ Googleのアプリパスワード作成

Googleアカウント>セキュリティ>Googleにログインする方法>二段階認証

手順④ Gmailに他のアドレスを追加する

support.google.com

Gmail > 設定 > 全ての設定 > アカウントとインポート