How Can I Redirect Queued Mail in Postfix?
July 13, 2007 3:04 PM   Subscribe

Can I redirect a queued message in postfix?

Ack. A user had forwarding set up to an exchange server that won't talk to the lowly likes of my postfix based mailserver. I've cleaned up their .forward file so that new messages are routed properly, but now there are 160 messages sitting in the queue that still want to go to the exchange server.

The folks running the server don't seem to be very forthcoming about getting things to work on their side, so I'm trying to figure out if there's something I can do to drain the queue into the user's local account.

Throwing the messages back into the queue just results in another attempt and timeout on the exchange server. If I could edit the queued messages in the /var/spool/postfix/deferred directory to change their destination, I should be able to get this cleaned up. However, queued messages aren't in plaintext.

Googling around hasn't shown me anyone dealing with anything similar.

Ultimately, I can use strings to grab content, but that's only a last resort solution for some of the messages if the user can give me specific things to look for for critical messages. I'm not going to do that with all 160 messages though.

Any ideas? Something that can be automated would be ideal.

Thanks!
posted by ursus_comiter to Computers & Internet (6 answers total)
 
postcat is your friend.
posted by nicwolff at 3:37 PM on July 13, 2007


Best answer: Alternatively, when you requeue messages with "postsuper -r", they are run through the address rewriting and substitution rules again. So I'd think that if you temporarily added a virtual rule for the user's address on the Exchange server to their local address, then requeued the messages, they should get rerouted. You may have to add the Exchange server's hostname tempararily to $mydestination to make Postfix accept the rule.
posted by nicwolff at 3:37 PM on July 13, 2007


Oh and now that I re-read, don't bother editing the messages in the queue - just postcat them into his mail spool, then delete them from the queue. And I do know how to spell "temporarily".
posted by nicwolff at 5:04 PM on July 13, 2007


nicwolff's method of adding the address to the virtual map and requeueing the messages should work. That's probably your easiest route. I don't think you can edit the messages in the queue. They would be marked as corrupted and not delivered. I don't think just postcatting them into his mailspool will work quite right, either, since postcat adds a bunch of queue information to the top of the message, screwing up the headers. You would need to strip that out. You could then resend the message to the new address. Something like this:

WARNING! DO NOT USE THIS! IT IS AN UNTESTED EXAMPLE FOR REFERENCE ONLY! (and please don't laugh at all the piping. It's a modification of a script I've been using for queue management that works, so I've never bothered cleaning it up. And it's just easier than actually learning proper regular expressions ;)

for i in $(mailq | tail +2 | grep -v '^ *(' | awk 'BEGIN { RS = "" } { if ($8 == "oldaddress@olddomain.com") print $1 }');do postcat -q $i | sed -n '/MESSAGE CONTENTS/,$p' | grep -v "MESSAGE CONTENTS"; | sendmail newaddress@newdomain.com; done

Inelegant, but it should work. It's just using awk to find messages to oldaddress in the mailq, and printing out a list of the message IDs. The for loop gives those IDs one at a time to postcat, whose output is piped through sed and grep to remove the queue info, so it can then push the clean message to sendmail with a new recipient address. Once you are satisfied that the messages have been delivered, you can delete the stuck messages from the queue with postsuper.
posted by team lowkey at 6:49 PM on July 13, 2007


The rewrite stuff is probably the right answer, but my inclination would be to try something a bit more kludgey: diddle your local DNS to convince your postfix server that the delivery address corresponds to your local server, then flush the queue.
posted by genehack at 7:41 PM on July 13, 2007


Response by poster: Re-routing via the virtual domain worked perfectly! Thanks!
posted by ursus_comiter at 9:18 AM on July 14, 2007


« Older How can I get a Grand Central invite?   |   Large scale scanning and printing? Newer »
This thread is closed to new comments.