0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

postfix で 全てのメールに共通の Envelope From を設定する

Last updated at Posted at 2025-10-05

動機

とあるISPでDDNSを利用してlinuxサーバーを運用しているが、メールリレーサービスが突然終了。仕方なく直接送信に戻したところ、宛先によって届かないケースが頻発。
色々調べた結果、認証情報を設定すればISPのSMTPサーバーを利用できることは分かったが、自分の利用環境では認証ユーザーのアドレスをEnvelope Fromに設定する必要があることが判明。

外部SMTPサーバーを使うための設定

1. cyrus-sasl-plainをインストール(RedHat系ディストリビューションの例)

bash
# dnf install cyrus-sasl-plain

2. 認証情報の設定
/etc/postfix/sasl_passwdを作成し、以下のように認証情報を記載する。サーバー名、ポート、ID、パスワードなどは環境に合わせて適宜。

sasl_passwd
[smtp.example.com]:587 env_from@isp.example.com:password

このファイルをchmod 600して、postmapコマンドでハッシュDBを作成。

bash
# chmod 600 /etc/postfix/sasl_passwd
# postmap /etc/postfix/sasl_passwd

3. main.cfの編集
/etc/postfix/main.cfに以下を追記する。

main.cf
relayhost = [smtp.example.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_tls_security_options = noanonymous
smtp_sasl_mechanism_filter = plain
smtp_tls_CApath = /etc/pki/tls/certs/ca-bundle.crt

これで、postfixをリロードすれば、ISPのSMTPサーバーを利用できるようになる。

Envelope From を書き換える設定

この状態で普通にメールを送信しようとすると、ローカルアドレスがEnvelope Fromに設定されることが原因で、SMTPサーバーの認証がはじかれてしまった。sendmail を使う場合は -f オプションで認証ユーザーのアドレスを設定すれば大丈夫だが、個別対応は面倒。ということで、以下のようにして全ての送信メールに共通のEnvelope Fromを設定できた。
1. /etc/postfix/sender_canonicalにアドレス変換のマッピングを設定

sender_canonical
/^.+$/ env_from@isp.example.com

ここでは、正規表現を用いて全てのアドレスを変換するようにしている。
2. main.cfの編集
/etc/postfix/main.cfに以下を追記する。

main.cf
canonical_maps = regexp:/etc/postfix/sender_canonical
canonical_classes = envelope_sender

2行目は、Envelope Fromのみを変換対象とするための設定(Header Fromは変換しない)
これでpostfixをリロードすれば良い。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?