2020/07/24

Postfix on MacOS

なんか試行錯誤してやっとたどり着いたので記録.自宅などで,private net に隠れた Mac から,プロバイダのSMTPサーバ経由で外にメールを送る設定をする手順.

まず,relayhost を main.cf に設定する.
relayhost = smtpserver.example.com:port#

あとは巷にあふれるSMTPの設定."Mac postfix gmail" とかで検索すると情報いっぱいある.
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
smtp_sasl_mechanism_filter = plain
smtp_use_tls = yes

で,最後にやっと解決できたのは,メールの from アドレスの書き換え.SMTP認証が通っても,from が正しくないと通してくれない.うちの場合は, "Sender address rejected: not owned by user xxx@xxxx.example.com" と言われました.ので,これを強制的に書き換えてあげる必要があって,sender_canonical_maps に変換テーブルを作ってあげる.
sender_canonical_maps = hash:/etc/postfix/sender_canonical
local_header_rewrite_clients = static:all

中身↓
naotaka@(hostname).local email-address-in-provider

% sudo postmap /etc/postfix/sasl_passwd
% sudo postmap /etc/postfix/sender_canonical

(余談)
From アドレスの書き換えには, main.cf の myhostname を書き換えることでも対応可能なようにも思えるのですが,そこでは,naotaka@$myhostname になってしまいます.この naotaka がプロバイダのアカウントと一致してないといけないので,弾かれてしまいました.かつ,xxx@$myhostname の xxx が Mac に存在しないと不達になるという罠もあるので, myhostname は無視で,sender_canonical が正解でした.

(残課題)
SMTPUTF8 is required, but was not offered by host [provider-smtp-server] というエラーは出ていて,どうやら subject に日本語が入っていると通らなくて,crontab でディレクトリに日本語が入ってるスクリプトを起動してると subject に日本語が入ってしまって送れないという罠が残ってます.これはsmtp server 側の機能不足?か,client でencode してあげれば良いだけ?

(参考) https://tech-lab.sios.jp/archives/12289