配置Postfix外发邮件速率限制以适配VPS中继主机规则
Got it, let's walk through setting up Postfix to mirror your relayhost's sending limits perfectly—this way you'll never accidentally hit those caps that trigger extra fees or account bans. Since you don't mind delayed emails, we'll configure Postfix to hold messages in the deferred queue instead of rejecting them when limits are reached or approached.
We'll use Postfix's built-in anvil service (for rate tracking) and transport-specific limits to enforce the three tiers of restrictions: 1000 emails/hour, 3000 emails/day, and 20000 emails/month.
Step 1: Define a Dedicated Relay Transport
First, we'll create a custom transport map to route all outbound emails through your relayhost. This lets us apply rate limits specifically to traffic going to that relay.
- Edit the transport map file (usually
/etc/postfix/transport):
Replace* relay:[your-relayhost-address][your-relayhost-address]with the actual hostname/IP of your provider's relay. - Compile the transport map into a hash table (required for Postfix):
postmap /etc/postfix/transport
Step 2: Configure Rate Limits in main.cf
Open your Postfix main configuration file (/etc/postfix/main.cf) and add or update these settings:
# Enable the anvil service to track message sending rates anvil_rate_time_unit = 60s # Tell Postfix to use our custom transport map transport_maps = hash:/etc/postfix/transport # Enforce relayhost-specific rate limits (matches your provider's constraints) # Hourly limit: 1000 messages per hour relay_destination_rate_limit = 1000/h # Daily limit: 3000 messages per day (stricter than hourly's 24k cap) relay_destination_rate_limit += 3000/d # Monthly limit: 20000 messages per 30 days relay_destination_rate_limit += 20000/30d # Limit concurrent connections to the relay to avoid overwhelming it relay_destination_concurrency_limit = 5 # Configure queue behavior to delay instead of reject messages # Disable hard retry limits (let messages stay in queue until they can send) relay_destination_retry_limit = 0 # Send a warning email if a message is delayed for 4 hours (adjust as needed) relay_destination_delay_warning_time = 4h # Allow messages to stay in the queue for up to 30 days (covers monthly limits) maximal_queue_lifetime = 30d # Wait 1 hour before retrying a delayed message (adjust based on your needs) smtp_destination_full_delay = 3600s
Step 3: Verify and Apply Changes
- Check for configuration errors:
postfix check - Reload Postfix to apply the new settings:
postfix reload
Step 4: Monitor Limits and Queue
- To check the deferred queue (delayed messages):
postqueue -p - To view rate tracking logs from the
anvilservice:postlog | grep anvil
This setup will ensure Postfix automatically holds messages in the deferred queue as soon as any of the relay's limits are approached, and retries them once the rate window resets. No more worrying about accidental overages!
内容的提问来源于stack exchange,提问作者Richard Brinkman

