postfix

Postfix Mail Queue Management

Postfix Mail System is the one of the most widely used mail systems along with Exim. In the initial days postfix was widely used for custom setup and custom Mail server setups. But nowadays Plesk servers also has Postfix as the default mail server and not Qmail. In this blog, we mainly concentrate on Mail Queue Management commands which almost all server owners and server administrator may need at some point of time.

Postfix has five different queues and they are listed below. All mails which postfix handles will stay in the server in one of these queues until the message leaves from the server.

  1. maildrop
  2. hold
  3. incoming
  4. active
  5. deferred
  6. Corrupt

You can get a detailed reference of all the above queues from this link. Postfix uses a separate directory for each of the above queues and the default directory for those are:

/var/spool/postfix/maildrop
/var/spool/postfix/hold
/var/spool/postfix/incoming
/var/spool/postfix/active
/var/spool/postfix/deferred
/var/spool/postfix/corrupt

The above is just a reference for the queue structure and below is the actual set of commands which a server owner or a server administrator needs to handle a Postfix Mail queue and I will also mention how to find out a spamming instance as well so that you can get a more detailed idea on postfix queue management.

Display the list of Queued mails , deferred mails, and Pending mails

# postqueue -p
Sample Output
[root@host1 ~]# postqueue  -p
-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient-------
C79CEC3F6BC*     526 Wed Dec  5 15:05:18  root@host1.server.com
test.test@gmail.com

In the above result, Queue ID is C79CEC3F6BC and we need this for all future checks

To Display the mail header and contents

# postcat -q “Queue ID”
# postcat -q C79CEC3F6BC

To check the total number of mails in the queue

# postqueue -p | grep -c "^[A-Z0-9]"

To reattempt delivery of all mails in the queue

# postqueue -f

To remove all Mails in the Queue

# postsuper -d ALL

To remove all mails in the deferred Queue

# postsuper -d ALL deferred

To remove particular mail in the queue.

# postsuper -d “Queue ID”
# postsuper -d C79CEC3F6BC

To remove all mails from a particular mail id

[email protected]

# mailq | tail +2 | awk 'BEGIN { RS = "" } / test.test@domain\.com$/ { print $1 }' |
tr -d '*!' | postsuper -d -

To attempt to send one particular mail

# postqueue -i “Queue ID”
# postqueue -I C79CEC3F6BC

To clear the infected mails by user or pattern

To clear the infected mails sent by a specific user or any specific pattern, you can use the below one. This will simply check that content which is searching and will remove all those emails which contains that pattern.

To remove all mails which have [email protected] in the entire mail.

# for id in `postqueue -p|grep '^[A-Z0-9]'|cut -f1 -d' '|sed 's/*//g'`; do postcat -q $id
| grep [email protected]  && postsuper -d $id; done

To remove all mails which have a particular pattern like  “X-PHP-Originating-Script: 48:badmailing.php” we can use the above script as below. When you are giving a longer pattern, make sure you copy paste all space and give all those exactly in the double quotes.

# for id in `postqueue -p|grep '^[A-Z0-9]'|cut -f1 -d' '|sed 's/*//g'`;
do postcat -q $id | grep “X-PHP-Originating-Script: 48:badmailing.php”
&& postsuper -d $id; done

Conclusion

I hope this article helps you get more comfortable with Postfix Mail Queue Management.

About the author

Sumesh Prabhu

I am Linux Server Administrator by Profession for the last 9 years and works mainly as a freelancer in Fiver and have a small hosting company mainly for my personal clients which I have acquired over the last many years . My personal site where I have started a Forum and Blog to discuss server related issues as well.
Site : ServeradminSupport.com
Blog : Serveradminsupport.com/blog
Forum : Serveradminsupport.com/forum
Linux Hosting : LavenderHost.com