Customize direct-delivery Postfix configuration

If you have a mail receiver container which requires customised Postfix configuration, this is the topic for you. Herein are described the steps required to set Postfix main.cf configuration variables to whatever your heart desires.

Postfix configuration variables can be set via the container environment. Any environment variable starting with POSTCONF_ will set a Postfix configuration variable named for the rest of the environment variable to the value of the environment variable. For example, if you set the environment variable POSTCONF_always_bcc to bob@example.com, then Postfix will be configured with always_bcc = bob@example.com, which will send a copy of all incoming mail to Bob. Poor Bob.

Procedure

  1. Figure out what configuration variables you want to set, and what values to set them to. This may be done by reading the fine manual, or through recommendations in other Discourse documentation, or otherwise.

  2. Connect to your Discourse server via SSH, grab some root privileges, and head over to where all the discourse-docker configuration lives:

    ssh ubuntu@192.0.2.42
    sudo -i
    cd /var/discourse
    
  3. Open up containers/mail-receiver.yml in your text editor of choice, and swing down to the env: section of the file. Somewhere in there, add entries for the variables you want to add, being careful to not modify anything else, and maintaining appropriate indenting. For example, if we were adding our always_bcc setting, the file might look a bit like this:

    env:
      LANG: en_US.UTF-8
      MAIL_DOMAIN: discourse.example.com
      DISCOURSE_BASE_URL: 'https://discourse.example.com'
      DISCOURSE_API_KEY: abcdefghijklmnop
      DISCOURSE_API_USERNAME: system
    
      POSTCONF_always_bcc: 'bob@example.com'
    

    Once you’re happy with what you’ve added, save and exit your editor.

  4. To load the configuration, you simply have to restart the mail-receiver container (a rebuild is not required):

    ./launcher restart mail-receiver
    

    After a brief spasm, the container should be running again.

  5. Test your changes. Ensure both that what you wanted to have happen has, indeed, happened, and also that nothing you didn’t expect to change hasn’t.

Addendum: adding files to the mail-receiver container

Many Postfix configuration parameters require access to “database files”, which provide key/value information which Postfix uses to make decisions about what do with mail. If you see that a configuration parameter accepts a filename that looks like hash:/some/file, you’ve found a use for database files.

The thing is, Postfix running inside the container needs to be able to get at those files while it’s running, which means you need to either copy those files into the container, or (preferably) put those files into a directory on the host, and then mount that directory as a volume inside the container. These instructions describe the second method.

Once you have completed this procedure, any file you place into /var/discourse/shared/mail-receiver/etc will immediately become visible at /etc/postfix/shared inside the container, and any changes you make to those files will be immediately visible to Postfix.

Here’s how to make it happen.

  1. If you’re not still logged in as root to your Discourse server, do so again:

    ssh ubuntu@192.0.2.42
    sudo -i
    cd /var/discourse
    
  2. Open up containers/mail-receiver.yml in your text editor of choice, and this time head for the volume: section. Underneath the existing definition for the /var/spool/postfix directory, add another one, so that your volume section looks like this:

    volumes:
      - volume:
          host: /var/discourse/shared/mail-receiver/postfix-spool
          guest: /var/spool/postfix
      - volume:
          host: /var/discourse/shared/mail-receiver/etc
          guest: /etc/postfix/shared
    

    Save/exit your editor.

  3. To attach the new volume, you simply have to restart the mail-receiver container (a rebuild is not required):

    ./launcher restart mail-receiver
    

All done!

「いいね!」 10

Matt, do you think that could be possible to enable accounts like admin@domain or info@domain from this Postfix configuration?

I only need to have a couple of addresses for incoming e-mail and I have it working with Discourse but I can’t set accounts (their seem to be blocked by default even though messages are processed).

Thanks for all your guides related.

Digital Oceanで試用のDiscourseサービスをセットアップし、送信メールにはMailgunを使用しました。適切なMXレコードがDigital OceanのIPアドレスを指すように設定されたドメインも登録済みです。Discourseでの送受信メールは正常に機能しています。トピックへの返信は、通知が設定されているユーザーに送信メールを生成し、テストユーザーはこれらのメールに返信して、投稿がDiscourseに表示されます。ここまでは順調です。

上記のようにPOSTCONF_always_bcc: オプションを追加しようとしましたが、機能していないようです。「mail-receiver」の部分が、Discourseの「app」部分がMailgun経由でメールを送信する方法を知っているにもかかわらず、Mailgun経由でメールを適切に送信できないのではないかと疑っています。app.ymlにはMailgunサーバーのユーザー名とパスワードが含まれていますが、この情報をmail-receiverの設定ファイルにどのように記述するかについての例を見たことがありません。

always_bccオプションが読み取られ、処理されていることはわかっています。なぜなら、以下を実行すると:

./launcher enter mail-receiver

そして実行する

mailq

送信しようとしているテストメッセージがキューにあり、配信されようとしているのを確認できます。「-Sender/Recipient-------」の列には、テストメッセージが送信されたアドレス、(unknown mail transport error) という言葉、そしてalways_bcc設定にあったメールアドレスが表示されます。

postmaster@mydomainまたはadmin@mydomainに送信されたメッセージを、Discourseでの処理のために送信するのではなく、公開インターネット経由でMailgun経由でGmailアドレスに再送信するように、受信メッセージをフィルタリングする方法を望んでいました。これはユーザー@satonotdeadが試みようとしていたことかもしれません。

これを実現するためのヒントをいただけると幸いです!

うーん。はい、まず、app.ymlの認証情報や転送メカニズムについて知らないメール受信者を、メールを転送する手段を持つように設定する必要があります。マウントボリュームに関する次のセクションで示唆されているように、より完全な設定を追加する必要があると思いますが、その詳細は本書の範囲を超えています。

postmasteradminのメールをどう扱えばよいか」という簡単な解決策は、それぞれにグループを作成し、そのメールを受け取りたい人をそのグループに追加すれば、グループメッセージとして処理できます。

「いいね!」 2

Mailgunではなく「mail-receiver」のことでしょうか?つまり、「mail-receiver」が公開インターネット経由でMailgunと通信する方法を教え、実際の配信を実行するように適切に認証情報を渡すということでしょうか?

はい。すみません。

まあ、そうですね、あるいは、何らかの方法で、mail-receiver(つまりPostfix)を設定してメールを配信させるということです。mail-receiverの使い方を知っているなら、それを使うよりもそちらを使った方が良いのではないかというのが私の意見です。

別の解決策としては、domainのメールを処理し、他のMXの下でmail-receiverに転送するような <mail thing> プロセスを用意することです。

数時間、様々な組み合わせを試しましたが、Discourseが実行されているコンテナの外にpostfixをインストールし、コマンドラインからMailgun経由で電子メールを送信できるようになりました。そのため、postfixをMailgunを使用するように正常に設定しました。しかし、リレーがMailgun経由で機能するように、mail-receiverコンテナに設定を組み込む方法については、まだ解決策が見つかっていません。簡単な方法があるはずです。メッセージがメールキューに留まっている理由を特定するためのログを見つけることができません。コンテナは、私がLinuxを使用していた最後(数年前)には存在しませんでした。Postfixが試みている通信を確認できるようにログを有効にする方法はありますか?そうすれば、問題がどこにあるのかを特定できます。概念的には、admin@mydomainが受信されたら、Mailgun経由で私の個人のGmailアカウントに直接送信され、category1@mydomainやcategory2@mydomainなどは、投稿の作成に使用するためにローカルのDiscourseにプッシュされるようにしたいと考えています。

「いいね!」 2

Discourse コンテナの外部、別の VPS またはデータセンターで mail-receiver を使用できますか?

目的は、プライバシー向上のために Discourse の IP アドレスを変更し、Discourse フォーラムと連携して動作/認証する外部の「mail-receiver」を使用することです。

はい。まさにそれを実行しています。メール受信者をDigitalOceanで実行し、Discourseを別のデータセンターのマシンで実行しています。

誰かやり方を説明してくれませんか?この人は答えてくれただけでお金を要求しています。

ご質問は何でしょうか?

メール受信者をサーバーに設定するために特別なことは何も必要ありません。Dockerがインストールされており、必要なポートにアクセスできる限り、問題なく動作します。

メール受信者をセットアップしましたが、メールを処理しようとすると404エラーが発生します。
私のサイトはサブドメインです。例:forum.site.com
そして、app mail-receiverの DISCOURSE_MAIL_ENDPOINT は次のようになっています。

DISCOURSE_MAIL_ENDPOINT: ‘http://forum.site.com/admin/email/handle_mail

Discourseも再構築する必要がありますか?

404エラーが発生している場合は、APIキーが間違っている可能性が高いです。

「いいね!」 2

デフォルトのAPIですが、まだ404が返ってきます。Google Talkでお送りしましたので、ご確認ください。

「いいね!」 1

SMTPバナーの設定方法

MXtoobox’s SuperTool でSMTPバナーチェックの問題が報告されています。
image

通常、EHLOバナーはMAIL_DOMAINと一致するはずであり、これはさらに逆引きDNSポインター(PTRレコード)と一致するはずです。したがって、私のmail-receiverdiscourse.exampleで実行されている場合、POSTCONF_myhostnamediscourse.exampleであるべきです。

EHLOバナーを設定する正しい方法はありますか?

最初に考えたのは、mail-receiver.ymlHOSTNAMEを設定して、/etc/postfix/mail-receiver-environment.jsonの元のhost-mail-receiver.localdomainを置き換えることでした。しかし、これは/etc/hostnameを変更せず、Postfixの設定のmyhostnameも変更しません。

POSTCONF_myhostnameを使用することを検討していますが、$myhostnameは複数の場所で使用されており、/etc/hostnameと一致しなくなるため、意図しない副作用があるのではないかと心配しています。

root@host-mail-receiver:/etc/postfix# postconf | grep myhostname
lmtp_lhlo_name = $myhostname
local_transport = local:$myhostname
milter_macro_daemon_name = $myhostname
myhostname = host-mail-receiver.localdomain
myorigin = $myhostname
smtp_helo_name = $myhostname
smtpd_proxy_ehlo = $myhostname
root@host-mail-receiver:/etc/postfix# cat /etc/hostname
host-mail-receiver

Discourse-setupが尋ねる設定があります。名前を覚えておらず、携帯電話で見つけるのが難しいです。ソースを見たり、実行したりすることができます。

Postfix の smtp_helo_name 設定は HELO (または EHLO) コマンドで指定された名前を変更しますが、これは 送信 配信の設定であり、SMTP バナーはメールを 受信 する際に送信されます。デフォルトのホスト名は myhostname から取得されますが、smtpd_banner 設定でバナーに表示されるものを変更できます。

「いいね!」 1

これが他の人に役立つかわかりませんが、私も同様の問題が発生していました。この情報のおかげで、何らかの理由でAPIキーを取り消していたことに気づきました。取り消しを取り消したところ、受信メールが再び機能するようになりました。

APIキーに関連していることに気づかせてくれてありがとう :slightly_smiling_face:

「いいね!」 1

これは書かれた時点から何か変更されたのでしょうか? env: の下に POSTCONF_smtpd_banner 値を追加しても、複数回再起動しても全く認識されませんでした。反映させるには再構築 (./launcher rebuild mail-receiver) が必要でした。