Rewrite address (To:) of message in postfix’s email queue

If an out-going email message gets stuck in postfix’s deferred queue due to a malformed To: (recipient) address, there are a couple of ways to deal with it.

First one is courtesy of Mattias (@mattiasgeniar), who introduced the postcat -h -b command (thanks Mattias!) to extract the queued message’s headers and body to a temporary file, then used that file as an input to sendmail:

sendmail -f

Read Mattias’s article, it’s good.

Others suggest to use postfix’s smtp_generic_maps in the file to rewrite the message next time delivery is attempted.

This method ought to work, but it wasn’t working as expected and the reason for this post is to help explain why, and how to overcome the issue.

In brief: since the queued message was addressed To: and $mydestination did not include, the rewriting rules in /etc/postfix/generic were not being invoked.

There’s a subtle hint in the postfix documentation:


user address

Replace  user@site  by  address when site is equal to $myorigin,
              when site is listed in $mydestination, or...

It’s still unclear why the preceding stanza in the documentation didn’t work:

user@domain address
              Replace user@domain by address. This form has the highest prece-

Changing to append “,” to the $mydestination= line, creating an /etc/postfix/generic file and running postmap generic in the /etc/postfix folder, then, finally, running postfix reload, and finally the queue cleared.

It could be argued that Mattias’s method is better / more elegant (noteworthy: it preserved the date/time of the outgoing message), the latter method might be preferable for a large quantity of messages or a more permanent solution to a recurring issue.

One should investigate why the malformed messages are being generated and rectify that (“Hello, smartctl“).

Happy computing, hope this helps someone…