LoginSignup
4
3

More than 3 years have passed since last update.

ubuntu 20.04 LTS server でpostfixをインストールしgmailをサーバに設定する

Posted at

システムに異常が発生した時にメールで通知したいのでpostfixをインストールしgmailサーバでメールを送るように設定しました。
メールサーバの設定は昔のsendmail設定の複雑怪奇さの印象しかないのでgmailサーバを使わせてもらいます。

Gmail 経由でメールを送信するように Postfix を設定する

設定ではこちらを参考にさせていただきました。というかそのままです。

postfixのインストール

$ sudo apt install postfix bsd-mailx libsasl2-modules
を実行して関連パッケージをインストールします。
設定画面が表示されるので、

  • インストール種別の選択肢でInternet with smarthost
  • SMTP relay hostに[smtp.gmail.com]:587

を指定します。

/etc/postfix/sasl_passwd
というファイルを作成してgmailの認証情報を記述します。
[smtp.gmail.com]:587 <username>@gmail.com:<password>
<>内は自分の環境に合わせてください。
パスワードは2段階認証を設定しているとエラーになるのでアプリパスワードを発行して使用してください。
[アプリパスワードの発行方法はこちら]

(https://support.google.com/accounts/answer/185833?hl=ja)

sasl_passwdファイル補作成したら

$ sudo chmod 600 /etc/postfix/sasl_passwd
$ sudo postmap /etc/postfix/sasl_passwd

を実行し、

/etc/postfix/main.cf

smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_tls_security_options = noanonymous

と追記します。

設定が終わったら
$ sudo systemctl restart postfix.service
でpostfixを再起動します。

動作確認

$ echo "test" | mail -s "subject" user@hoge.com
みたいなコマンドを実行してみます。
メールアドレスは送りたいメールアドレスを指定してください。

メールが届けば成功です。
var/log/mail.logにログが残っているはずなので気になる方は確認してください。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3