sendmail - izudon/izudon.github.io GitHub Wiki

Gmail からメール送信できるようにする

さくら VPN などのインターネット上に位置するサーバに、
Gmail を通じて(SMTP)メール送信できるように設定する方法。

ポイント

  • SASL に対応する必要がある。

*** 重要 ***

2022.05.30 より、ユーザ名とパスワードだけで Google アカウントを
利用しようとするアプリケーションは弾かれるようになった。

Feb  6 21:09:24 os3-373-19946 postfix/smtp[67248]: 4EC406642C: SASL authenticati
on failed; server smtp.gmail.com[64.233.189.109] said: 534-5.7.9 Application-spe
cific password required. Learn more at?534 5.7.9  https://support.google.com/mai
l/?p=InvalidSecondFactor cj23-20020a056a00299700b0058ba2ebee1bsm6927934pfb.213 -
 gsmtp

1. Postfix をインストールする

$ sudo apt install postfix
  • Please select the mail server configuration type...
    (eメールのコンフィギュレーションタイプを聞かれる)
    • Internet Site(インターネットサイト)を選択。
  • FQDN を入れてくれと聞かれる。
    • 普通に答える。

2. SASL の設定ファイルを用意する

パスワードを記載したファイルを用意する

$ sudo touch           /etc/postfix/sasl/sasl_passwd
$ sudo chown root:root /etc/postfix/sasl/sasl_passwd
$ sudo chmod 600       /etc/postfix/sasl/sasl_passwd
$ sudo vi              /etc/postfix/sasl/sasl_passwd

/etc/postfix/sasl/sasl_passwd

[smtp.gmail.com]:587 [email protected]:P@ssW0rd
  • サーバ名、ポート番号、アカウント名、パスワード、を記載。
$ sudo postmap /etc/postfix/sasl/sasl_passwd
$ sudo ls      /etc/postfix/sasl/
  • /etc/postfix/sasl/sasl_passwd.db というファイルができている。

3. Postfix の設定ファイルに記載する

$ sudo vi /etc/postfix/main.cf
  1. relayhost = という行を見つけそこに、
    relayhost = [smtp.gmail.com]:587 のように追記。
  2. ファイル末尾に SASL 利用の設定を追記。
    # Enable SASL password
    smtp_sasl_auth_enable = yes
    smtp_sasl_security_options = noanonymous
    smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_passwd
    smtp_tls_security_level = encrypt
    smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
    
    • /etc/ssl/certs/ca-certificates.crt は、
      OSプリインストールのおそらくルート証明書の束。
  3. Postfix 再起動
    $ sudo systemctl restart postfix
    

4. 以上で普通に sendmail でメール送信できるようになる

$ sendmail [email protected]
Subject: somesubject

Hello.
$

inet_interfaces = loopback-only

資料