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:
- Find the e-mail
- Mark mail as ‘on hold’ in the Postfix queue
- Extract the mail from the queue
- Requeue to a different recipient
- 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 'm@ttias.be' -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.