Detaching from Postfix MailQ

You maybe know this scenario: At some sunny morning some guy using your mailservers decides to send a reaal big mailing via you and has forgotten to be polite and to remove all the recipients who does not exist any more (as in: adress closed) and even less polite: to remove all recipients who want to be removed! Yes, this is Spam, and it runs through your mailservers.

Now you have to clean your queue and to remove all those unwanted mails. Thats where the problem starts: with the postfix standard tools its quite slow and a terrible lot of work. The “normal” approach, to simply run mailq, copy the ID and remove via postsuper -d ID of course has its limits for – lets say – 10 Mails. Then it becomes a pain in the ass. Ok, what else can you do ? Remove all deferred mails, as in postsuper -d ALL defer, but is this really what you want ? There could be also a lot of mails from other clients as well (which are queued because of the big mailing).

Ok, here is what i wanted: removing all mails for a certain sender (a single email or a whole domain) to a certain recipient (in some cases, sometimes all), but, and this is really important to me, dont simply remove those mails. I want a save them, so that i can re-inject them later on or zip them and give them back to sender.

Thus i wrote a perl script (attached below) which achieves all of this and a little more. Following some examples what it can do:

# Remove a single mail with the id ID from the queue and detach it in the /tmp/mailq-detach-directory
postfix-detach-from-queue -i ID -u -o /tmp/mailq-detach/
# Remove all mails addressed to some-domain.tld and detach to ..
postfix-detach-from-queue -t some-domain.tld -u -o /tmp/mailq-detach/
# Remove all mails from sender-domain.tld and detach to ..
postfix-detach-from-queue -f sender-domain.tld -u -o /tmp/mailq-detach/
# Dump the whole mailq (for performance reasons):
mailq 〉 /tmp/mailq.dump
# Then use it as a source and remove all mails from some@sender.tld to domain.tld and detach to ..
postfix-detach-from-queue -q /tmp/mailq.dump -f some@sender.tld -t domain.tld -u -o /tmp/mailq-detach/

Ok, thats how it works. For full documentation you can run postfix-detach-from-queue -h which shows all options, including dry-run and verbosity stuff.

An important issue is:

  • Use the mailq.dump for large queues OR dont use the -f sender@domain.tld parameter, because it had to parse the mails from your postfix deferred diretctory via postcat, which is very, very slow

Thats all, here it is:

postfix-detach-from-queue

Of course the disclaimer: No garranties or warranties of any kind are given. The script may work or may not. Terrible things can happen as destroying your whole filesystem, deleting mails from queue which you dont want to be deleted or detaching the moon from the earth orbit. Use it with care!

2 Comments

  1. Alan says:

    I know this is an ancient post, but is there a way to reverse the

    postfix-detach-from-queue -f sender-domain.tld -u -o /tmp/mailq-detach/

    command? IE: re-insert the saved messages back into the postfix queue?

  2. uk says:

    Hello Alan

    Try something like:

    cat /tmp/mailq-detach/some-mail.eml | sendmail recipient@domain.tld

    This should do it. Probably encapsulate it in a for-loop, and use a grep+sed cmd to extract the recipient.

    Greets
    Ulrich

Leave a Reply

CAPTCHA image