Postfix Mail Queue Management

29/12/2020
Chưa phân loại
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 = "" } / [email protected]$/ { 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 “XPHPOriginatingScript: 48:badmailing.php”
&& postsuper d $id; done

Conclusion

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

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Upgrade Ubuntu From Command Line

Ubuntu is a popular Linux distribution for home usage as well as enterprise users.  Many users of Ubuntu rely on the Graphical...
12/02/2020

Setting up Libvirt QEMU and KVM stack on Debian

QEMU-KVM based virtualization is at the heart of Linux based virtualization stack. It is free and open source and is one...
29/12/2020

[DirectAdmin] Hướng dẫn upload website lên Direct Admin (phần 3)

Direct Admin (DA) là công cụ để quản trị hosting tuyệt vời, trong các phần giới thiệu trước đây...
30/12/2020