Postfix mail queue: deliver e-mail to an alternate address

Want to help support this blog? Try out Oh Dear, the best all-in-one monitoring tool for your entire website, co-founded by me (the guy that wrote this blogpost). Start with a 10-day trial, no strings attached.

We offer uptime monitoring, SSL checks, broken links checking, performance & cronjob monitoring, branded status pages & so much more. Try us out today!

Profile image of Mattias Geniar

Mattias Geniar, August 02, 2016

Follow me on Twitter as @mattiasgeniar

Have you ever had an e-mail stuck in a postfix queue you’d just like to re-route to a different address? This could be because the original to address has e-mail delivery issues and you really want to have that e-mail delivered to an alternate recipient.

Here’s a couple of steps to workaround that. It basically goes:

  1. Find the e-mail
  2. Mark mail as ‘on hold’ in the Postfix queue
  3. Extract the mail from the queue
  4. Requeue to a different recipient
  5. Delete the original mail from the queue after confirmed delivery

It’s less work than it sounds. Let’s go.

Find the mail ID in postfix

You need to find the mail ID of the mail you want to send to a different address.

$ postqueue  -p | grep '' -B 2 | grep 'keyword'
42DA1C0B84D0    28177 Tue Aug  2 14:52:38  thesender@domain.tld

In this case, a mail was sent to me and I’d like to have that be delivered to a different address. The identifier in the front, 42DA1C0B84D0 is what we’ll need.

Mark the postfix queue item as ‘on hold’

To prevent Postfix from trying to deliver it in the meanwhile.

$ postsuper -h CF452C1239FB
postsuper: CF452C1239FB: placed on hold
postsuper: Placed on hold: 1 message

Don’t worry, your mail isn’t deleted.

Extract the mail from the queue

Extract that email and save it to a temporary file. If you’re paranoid, don’t save to /tmp as everyone can read that mail while it’s there.

$ postcat -qbh CF452C1239FB > /tmp/m.eml

Now, to resend.

Send queued mail to different recipient

Now that you’ve extracted that e-mail, you can have it be sent to a different recipient than the original.

$ sendmail -f $sender $recipient < /tmp/m.eml

Replace $sender and $recipient with real values. The sender should remain the same as the from address you saw with the postqueue -p command, the $recipient can be your modified address. For instance, in my example, I could do this.

$ sendmail -f thesender@domain.tld newrecipient@domain.tld < /tmp/m.eml

After a while, that mail should arrive at the new address.

Delete the ‘on hold’ mail from the postfix queue

After you’ve confirmed delivery to your new e-mail address, you can delete the mail from the ‘on hold’ queue in Postfix.

Warning: after this, the mail is gone forever from the postfix queue!

$ postsuper -d  CF452C1239FB
postsuper: CF452C1239FB: removed
postsuper: Deleted: 1 message

$ rm -f /tmp/m.eml

And you’re good: you just resent a mail that got stuck in the postfix queue to a different address!

Fyi: there are alternatives using Postfix’s smtp_generic_maps, but call me old fashioned - I still prefer this method.

Want to subscribe to the cron.weekly newsletter?

I write a weekly-ish newsletter on Linux, open source & webdevelopment called cron.weekly.

It features the latest news, guides & tutorials and new open source projects. You can sign up via email below.

No spam. Just some good, practical Linux & open source content.