For webmasters in the real world, the delivery retry function of postfix is troublesome sometimes. If you have a cron job generating emails all day and all night, and some recipients are unreachable or postfix service stops accidentally, many emails will be queued in deferred queue(/var/spool/postfix/deferred/). The worst result is when you restart the postfix service, postfix will create numerous connections to send the deferred emails, and your VPS gets suspended due to suspicious email spamming behavior. If you are an email marketer doing bulk mailing, you also do not want to send emails repeatedly to dead email addresses.
How to prevent postfix from keeping sending deferred emails? One way is to use a cron job to delete deferred emails from queue:
*/5 * * * * /usr/sbin/postsuper -d ALL deferred
This cron job will clean up deferred email queue every 5 minutes. If you want to keep the email copies, you can move the emails from deferred queue to the hold queue in the cron job as follows:
*/5 * * * * /usr/sbin/postsuper -h ALL deferred
Using a cron job to stop postfix from delivering deferred emails sounds a little silly. Postfix itself must have a configuration option to avoid endless retrying to send deferred emails. The configuration parameter is maximal_queue_lifetime. The parameter controls how long postfix should retry to send the deferred email before considering it as undeliverable and ceasing retry attempts. If you set maximal_queue_lifetime to 0, postfix only sends email once and never retries. If you are more conservative, you can set up postfix as in this post:
# if you can’t deliver it in an hour – it can’t be delivered!
maximal_queue_lifetime = 1h
maximal_backoff_time = 15m
minimal_backoff_time = 5m
queue_run_delay = 5m
This configuration instructs postfix to keep emails in deferred queue for no longer than 1 hour(much better than the default 5 days). Postfix scans the deferred queue every 5 minutes and the delivery retry interval is between 5 minutes and 15 minutes. Here is the more information about postfix configuration and postfix debug.