Inspecting Postfix’s email queue

Inspecting Postfix’s email queue.

This post explains how to view messages in the postfix queue, another post on this blog explains how to delete or selectively delete from the postfix queue

1- Postfix maintains two queues, the pending mails queue, and the deferred mail queue,
the differed mail queue has the mail that has soft-fail and should be retried (Temporary failure),
Postfix retries the deferred queue on set intervals (configurable, and by default 5 minutes)

In any case, the following commands should be useful

1- Display a list of queued mail, deferred and pending

mailq

or

postqueue -p

To save the output to a text file you can run

mailq > myfile.txt

or

postqueue -p > myfile.txt

the above commands display all queued messages (Not the message itself but the sender and recipients and ID), The ID is particularly useful if you want to inspect the message itself.

2- View message (contents, header and body) in Postfix queue

Assuming the message has the ID XXXXXXX (you can see the ID form the QUEUE)

postcat -vq XXXXXXXXXX

Or to save it in a file

postcat -vq XXXXXXXXXX > themessage.txt

3- Tell Postfix to process the Queue now

postqueue -f

OR

postfix flush

4- Delete queued mail

Delete all queued mail

postsuper -d ALL

Delete differed mail queue messages

(The ones the system intends to retry later)

postsuper -d ALL deferred

Delete from queue selectively

To delete from the queue all emails that have a certain address in them, we can use this program (perl script)…

NOTE: This perl script seems to be free, and is all over the internet, i could not find out where it originates or who wrote it.

1- Download this file, unzip, and upload the file to your server, then from your bash command line, Change Directory to wherever you uploaded this file, for example cd /root (Just an example, You can upload it wherever you wish)

NOTE: A second script here works differently, i have not yet tested it, download it here

Now, from within that directory, execute…

./postfix-queue-delete.pl anyaddress@example.com

Any mail that has this email address in it’s IN or OUT list will be deleted

The script uses the postqueue -p then looks for your string, once found, it deletes the email by ID, this means that this script can delete messages using any text that appears when you run mailq (or postqueue -p), so if you run it with the parameter joe all mail with addresses such as joefriend@example.com and

Other moethods exist, like executing directly

mailq | tail +2 | grep -v '^ *(' | awk  'BEGIN { RS = "" } { if ($8 == "email@address.com" && $9 == "") print $1 } ' | tr -d '*!' | postsuper -d -

——————————–

Sample Messages in a differed mail queue

——————————–

SOME282672ID 63974 Mon Nov 29 05:12:30 someaddresss@yahoo.com
(temporary failure. Command output: maildrop: maildir over quota.)
localuser@exmple.com

———————————-

SOME282672ID 9440 Wed Jun 30 05:30:11 MAILER-DAEMON
(SomeHostName [xxx.xxx.xxx.xxx] said: 452  Mailbox size limit exceeded (in reply to RCPT TO command))
username@example.org

———————————-

SOME282672ID 4171 Thu Nov 25 13:22:03 MAILER-DAEMON
(host inbound.somedomain.net [yyy.yyy.yyy.yyy] refused to talk to me: 550 Rejected: 188.xx.179.46, listed at http://csi.cloudmark.com/reset-request for remediation.)
someuser@example.com

———————————

SOME282672ID 37031 Thu Nov 25 08:53:36 someuser@example.net
(Host or domain name not found. Name service error for name=example.com type=MX: Host not found, try again)
someuser@example.com 

9 thoughts on “Inspecting Postfix’s email queue”

  1. Hi,

    I am running a postfix server with multi domain.

    I am facing lot of delay in my mails, and found around 80 emails are in mailq as deferred because of connection time out, even some of them for gmail as well. When I try to telnet gmail on port 25 it works occasionally. Sometime I am able to connect to gmail by telnet some time not. Can you please guide me in this.

    1. It could be gmail temporarily banning your mail server due to a perceived flood. There are postfix options available for throttling connections to the same recipient domain. See this option in the postfix configuration docs.

    2. Is your reverse DNS properly configured? This usually the main source for mail delays. You could (as a work around, and for testing) add the mail servers to /etc/hosts, then force Postfix to look at the hosts file first by adding this line to main.cf:

      smtp_host_lookup=native,dns

  2. Hi,

    I have a little question.
    I also use postfix now there are over 3600 mails in the queue.
    It’s not so easy to see who is the spammer … .

    Is there an possibility to see for example who appears the most in the queue?

    Thanks!

    1. Try with:
      – qshape

      More specific:
      – qshape -s deferred | head ===> deferred mails based on SENDER
      – qshape deferred | head ===> deferred mails based on RECEIVER

  3. hi there, all my mails are being held in queue and I have to manually re queue (postsuper -r ALL) it for it to be delivered. What shall I do?

Leave a Reply

Your email address will not be published. Required fields are marked *