送信用メールサーバ設定(django利用)

送信用メールサーバ(Postfix)構築・設定手順まとめ
以下は、VPS上で独自ドメイン(例: mail.ドメイン名)を使った送信用メールサーバ(SMTPサーバ)を構築し、外部アプリケーション(例: Django)から利用できるようにするまでの標準的な手順です。
1. 必要パッケージのインストール
sudo apt update
sudo apt install postfix mailutils
- インストール時、「Internet Site」を選択し、システムメール名にFQDN(例: mail.ドメイン名)を入力。
2. Postfixの基本設定
/etc/postfix/main.cf
を編集し、以下の項目を確認・設定。
- サーバのホスト名
myhostname = mail.ドメイン名
- サーバのドメイン名
mydomain = ドメイン名
- メールの送信元アドレスのドメイン
myorigin = /etc/mailname
/etc/mailname
の内容も mail.
ドメイン名 になっていることを確認
- ネットワークインターフェース
inet_interfaces = all
(外部からも接続する場合は all
、ローカルのみなら loopback-only
)
3. Submissionポート(587)の有効化
/etc/postfix/master.cf
を編集し、以下の行のコメントを外す。
submission inet n - y - - smtpd
編集後、Postfixを再起動:
sudo systemctl restart postfix
4. ファイアウォール設定
VPSやOSのファイアウォールで必要なポート(25, 587, 必要なら465)を開放。
例(ufwの場合):
sudo ufw allow 25/tcp
sudo ufw allow 587/tcp
5. TLS証明書の設定(推奨)
Let's Encryptなどで証明書を取得し、main.cfにパスを設定。
smtpd_tls_cert_file=/etc/letsencrypt/live/mail.ドメイン名/fullchain.pem
smtpd_tls_key_file=/etc/letsencrypt/live/mail.ドメイン名/privkey.pem
smtpd_use_tls=yes
6. 動作確認
サーバ上または外部から以下で接続テスト:
telnet mail.ドメイン名 587
バナーが返ればOK。
7. アプリケーション側の設定(例: Django)
EMAIL_HOST = 'mail.ドメイン名'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'メールアカウント'
EMAIL_HOST_PASSWORD = 'パスワード'
EMAIL_USE_TLS = True
8. 送信テスト
サーバ上でテストメール送信:
echo "Test mail body" | mail -s "Test Subject" [email protected]
9. ログ・トラブルシューティング
- Postfixの状態確認:
sudo systemctl status postfix
- ログ確認:
tail -f /var/log/mail.log
ポイントまとめ
- Postfixインストール時は「Internet Site」を選択
- main.cf/master.cfでドメイン・ポート・TLSを正しく設定
- ファイアウォールでポート開放
- アプリケーションから587/TLSで接続
この流れで、独自ドメインの送信用メールサーバを安全かつ確実に構築できます。
情報源
[1] How to Configure Postfix as a Send-Only SMTP Server on Ubuntu https://www.atlantic.net/vps-hosting/how-to-configure-postfix-as-a-send-only-smtp-server-on-ubuntu/
[2] Install and configure Postfix - Ubuntu Server documentation https://ubuntu.com/server/docs/install-and-configure-postfix
[3] How To Install and Configure Postfix as a Send-Only SMTP Server … https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-postfix-as-a-send-only-smtp-server-on-ubuntu-22-04
[4] How to configure a SMTP mail server with Postfix on Ubuntu 20.04 https://www.arubacloud.com/tutorial/how-to-configure-a-smtp-mail-server-with-postfix-on-ubuntu-20-04.aspx
[5] Setting Up and Configuring an SMTP Server with Postfix on Ubuntu https://99rdp.com/how-to-install-and-configure-postfix-on-ubuntu/
[6] How to Configure Postfix to Use External SMTP | phoenixNAP KB https://phoenixnap.com/kb/postfix-smtp
[7] Set up Postfix for incoming email - GitLab Docs https://docs.gitlab.com/administration/reply_by_email_postfix_setup/
[8] Installing the Postfix Mail Server on Ubuntu Linux - YouTube https://www.youtube.com/watch?v=9ubRcA4tFg4