Postfix でメールサーバーを構築

Postfix をインストールしてサーバーからメールを送信できるようにします。今回は送信専用として設定します。

サーバー
LXD ゲスト
CPU
2コア
メモリー
4GB
OS
Ubuntu Server 24.04 LTS 64Bit
apt パッケージ バージョン
postfix 3.8.6-1build2 amd64

パッケージをインストール

以下のコマンドで Postfix をイストールします。

sudo apt install postfix

インストール中に以下の設定をウィザードで確認されるので入力します。

General mail configuration type: Internet Site

System mail name:  example.com

Postfix を設定

以下のコマンドでウィーザードを使って Postfix を設定します。

sudo dpkg-reconfigure postfix

Recipient for root and postmaster mail: <USER_NAME>

Other destinations to accept mail for (blank for none):  <DOMAIN>

Force synchronous updates on mail queue? No

Local networks: 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 192.168.1.0/24

Mailbox size limit (bytes): 0

Local address extension character: +

Internet protocols to use: all

以下のコマンドでメールボックス形式を Maildir に変更します。実際の設定値は /etc/postfix/main.cf に保存されているので、このファイルを直接編集することもできます。

sudo postconf -e 'home_mailbox = Maildir/'

以下のコマンドで外部からの接続を無効にします。

sudo postconf -e 'inet_interfaces = loopback-only'

以下のコマンドで TLS の設定をします。Let’s Encrypt で発行された無料 SSL 証明書を使用しています。

sudo postconf -e 'smtp_tls_security_level = may'
sudo postconf -e 'smtpd_tls_security_level = may'
sudo postconf -e 'smtp_tls_note_starttls_offer = yes'
sudo postconf -e 'smtpd_tls_key_file = /etc/letsencrypt/live/foo.example.com/privkey.pem'
sudo postconf -e 'smtpd_tls_cert_file = /etc/letsencrypt/live/foo.example.com/cert.pem'
sudo postconf -e 'smtpd_tls_loglevel = 1'
sudo postconf -e 'smtpd_tls_received_header = yes'
sudo postconf -e 'myhostname = mail.example.com'

以下のコマンドで Postfix を再起動します。

sudo systemctl restart postfix.service

メール送信テスト

以下のコマンドでメールを送信してテストします。From アドレスを適切に設定しないと受信側でスパムメールとして識別される恐れがあります。

sendmail -t <<EOF
From: sender@example.com
To: recipient@example.com
Subject: テストメール

これはテストメールの本文です。
EOF

参考

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です