Postfix能否配置为Milter不可用时暂存消息直至其恢复?
Absolutely, you can set up Postfix to immediately return a 250 Queued for delivery response to sending clients, while holding messages in the queue until your Java-based standalone Milter is back online. The key is to shift your Milter execution from the SMTP session phase to the queue processing phase, so Postfix doesn't block the client when the Milter is down.
Step 1: Adjust Milter Placement in Postfix Configuration
Instead of configuring your Milter in smtpd_milters (which runs during the SMTP client session), move it to non_smtpd_milters. This tells Postfix to run the Milter after the message has been accepted into the queue, not during the client's connection.
Update your main.cf file with these settings:
# Remove or comment out your existing smtpd_milters line (if any) # smtpd_milters = inet:localhost:12345 # Configure Milter to run during queue processing non_smtpd_milters = inet:localhost:12345 # Replace with your Milter's IP/port milter_protocol = 6 # Required for queue-stage Milter support milter_default_action = tempfail # Treat Milter unavailability as a temporary failure milter_connect_timeout = 10s # Short timeout to detect Milter outages quickly milter_command_timeout = 60s # Give Milter time to process messages when available
Step 2: Understand What Each Setting Does
non_smtpd_milters: This triggers the Milter during the queue's delivery phase, not when the client is sending the message. Clients get their 250 response right away, no matter the Milter's state.milter_protocol = 6: Ensures Postfix uses a Milter protocol version that supports queue-stage processing (older versions only work during SMTP sessions).milter_default_action = tempfail: When Postfix can't reach the Milter, it treats this as a temporary error instead of rejecting or discarding the message. The message stays in the queue for retries.- Timeout settings: These make Postfix quickly detect when the Milter is down, so it can fall back to queuing instead of waiting indefinitely.
Step 3: Verify Milter Compatibility
Make sure your Java-based Milter supports Milter protocol version 6. Most modern Milter implementations (including Java wrappers for OpenMilter) do, but it's worth checking your Milter's documentation to confirm it can handle queue-stage requests.
Step 4: Test the Setup
- Stop your Java Milter service.
- Send a test email from a client—you should get a
250 Queued for deliveryresponse immediately. - Check Postfix's queue with
postqueue -p; the message should be listed as deferred (waiting for retry). - Start your Milter service again. Postfix will automatically retry the queued messages once the Milter is reachable.
Bonus: If You Need SMTP-Stage Milter Checks Too
If you still want some Milter checks during the SMTP session (like sender validation), you can keep smtpd_milters configured alongside non_smtpd_milters, but set milter_default_action = accept. This way:
- If the Milter is down during the SMTP session, Postfix accepts the message into the queue (client gets 250).
- The
non_smtpd_miltersconfiguration will re-run the Milter once it's back online, ensuring the message is processed properly.
Just note that this might result in the message being processed by the Milter twice (once during SMTP if available, once during queue processing), so ensure your Milter handles this idempotently (doesn't duplicate actions like adding headers).
内容的提问来源于stack exchange,提问作者Sathish Kumar

