ぷっぽプログラミング備忘録

linux, javascript, php, C#, そのうちScalaとかアプリ開発とかやってみたいです。

Ubuntu + docker+postfix+gmail経由でメールを送信する

Docker上のコンテナ内から、postfixを使ってgmail経由でメールを送信する方法です。

(受信とかはできません)

 

1. Googleアカウントの「2段階認証プロセス」を有効化する

 

2. Googleアカウントでアプリパスワード(※1)を取得する

※16ケタぐらいの*ログインパスワードではない自動生成されるパスワード*

 

3.postfixインストール
apt install -y postfix

 

4. main.cfを修正する(下記内容を追記)

vi /etc/postfix/main.cf

 

【内容 】

#Gmail Settings
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/gmail_passwd
smtp_sasl_security_options = noanonymous
smtp_sasl_mechanism_filter = plain
smtp_use_tls = yes

 

5. パスワードファイルを作成

vi /etc/postfix/gmail_passwd

【内容】

[smtp.gmail.com]:587 自分のgmailのアカウント名@gmail.com:アプリパスワード(※1)

 

例)

[smtp.gmail.com]:587 hogehogeo@gmail.com:wwwwxxxxyyyyzzzz

 

6. パスワードファイル登録

postmap /etc/postfix/gmail_passwd

 

7. パスワードファイル削除

rm /etc/postfix/gmail_passwd

 

8. Postfix の再起動

service postfix restart

もしくは
service postfix stop

service postfix start

 

9. mailxのインストール

apt install -y bsd-mailx



10. テストメールを送信してみる

echo "test from myserver" | mail -s "test mail" hogehogeo@gmail.com


実際に、メールが届いていたら成功です。(例ではhogehogeo@gmail.com)

参考)

【2021年版】PostfixからGmail経由でメールを送信する方法