habibzain Just husband, father and enthusiastic men about System Administration. Love to write short article about it. Perhaps can help and be useful for others.

PFDel Script Delete Mail Queue Postfix and Zimbra

2 min read

PFDel-Script-Delete-Mail-Queue-Postfix-and-Zimbra

PFDel is a term for bash script postfix whose contents commands to delete the email queue based on the sender’s or recipient’s address. Pfdel very useful for clearing email queues by sender or recipient only in postfix or zimbra mail server. No need to delete all queues.

Why Need Delete Mail Queue

Mail queues are emails deferred on the mail server for some reason. Email can’t be sent immediately, and still in the queue list until mail server retry send to destination address.

If we not deleting mail queue, the mail server will continuously try to send to the destination address for a maximum of 5 days based on the default postfix queue lifetime setting. This case will burdening the server work.

Some reason mail deferred is:

  1. Connection Refused to Destination.
  2. Refused to talk to me: 421 connection rate limit exceeded.
  3. Mail flooding caused spam attack.

Postfix or Zimbra Check Queue Lifetime

Simple command to check queue lifetime like below.

For postfix.

[root@mx-2 ~]# postconf -d | grep maximal_queue_lifetime
maximal_queue_lifetime = 5d
PFDel Script Delete Mail Queue

For zimbra.

[root@mail ~]# su - zimbra
Last login: Tue Sep 27 08:40:00 WIB 2022 on pts/0
[zimbra@mail ~]$ postconf -d | grep maximal_queue_lifetime
maximal_queue_lifetime = 5d

PFdel Script for Delete Mail Queue

For Postfix

Create file with name pfdel. Then chmod +x pfdel.

cd /home/user/
vi pfdel

Fill with script below.

#!/usr/bin/perl -w
#
# pfdel - deletes message containing specified address from
# Postfix queue. Matches either sender or recipient address.
#
# Usage: pfdel <email_address>
#

use strict;

# Change these paths if necessary.
my $LISTQ = "/usr/sbin/postqueue -p";
my $POSTSUPER = "/usr/sbin/postsuper";

my $email_addr = "";
my $qid = "";
my $euid = $>;

if ( @ARGV !=  1 ) {
    die "Usage: pfdel <email_address>\n";
} else {
    $email_addr = $ARGV[0];
}

if ( $euid != 0 ) {
        die "You must be root to delete queue files.\n";
}


open(QUEUE, "$LISTQ |") ||
  die "Can't get pipe to $LISTQ: $!\n";

my $entry = <QUEUE>;    # skip single header line
$/ = "";        # Rest of queue entries print on
            # multiple lines.
while ( $entry = <QUEUE> ) {
    if ( $entry =~ / $email_addr$/m ) {
        ($qid) = split(/\s+/, $entry, 2);
        $qid =~ s/[\*\!]//;
        next unless ($qid);
        # Execute postsuper -d with the queue id.
        # postsuper provides feedback when it deletes
        # messages. Let its output go through.
        #
        if ( system($POSTSUPER, "-d", $qid) != 0 ) {
            # If postsuper has a problem, bail.
            die "Error executing $POSTSUPER: error " .
               "code " .  ($?/256) . "\n";
        }
    }
}
close(QUEUE);

if (! $qid ) {
    die "No messages with the address <$email_addr> " .
      "found in queue.\n";
}

exit 0;

Save the file and change permission with chmod so that it can be executed.

chmod +x pfdel

For Zimbra

PFdel in zimbra mail server just use same script, need edit this parameter. Please attention in this line.

my $LISTQ = "/usr/sbin/postqueue -p";
my $POSTSUPER = "/usr/sbin/postsuper";

In Zimbra 8.60 old, change this parameter like below.

my $LISTQ = "/opt/zimbra/postfix/sbin/postqueue -p";
my $POSTSUPER = "/opt/zimbra/postfix/sbin/postsuper";

In Zimbra 8.8.x later.

my $LISTQ = "/opt/zimbra/common/sbin/postqueue -p";
my $POSTSUPER = "/opt/zimbra/common/sbin/postsuper";

How to Use Pfdel Script

Make sure file can be execute. To use pfdel script just run in command line.

./pfdel [email protected]

Can be use address mail sender or recipient in mail queue. For example, check mail queue in postfix.

[habibza@mx-2 ~]$ mailq

E2B2D2BEB13  121988 Tue Sep 27 08:49:13  [email protected]
(host mta6.am0.yahoodns.net[67.195.204.77] said: 421 4.7.0 [TSS04] Messages from 202.123.123.123 temporarily deferred due to unexpected volume or user complaints - 4.16.55.1; see https://postmaster.yahooinc.com/error-codes (in reply to MAIL FROM command))
                                         [email protected]

From the results of mailq above, we know that there is a queue of emails. We can delete emails from the sender, or from the recipient.

./pfdel [email protected]
or
./pfdel [email protected]

Ok, that is our article about pfdel script delete mail queue. May be it’s helpful, please feel free to leave a comment if you have any questions and I’ll appreciate it.


habibzain Just husband, father and enthusiastic men about System Administration. Love to write short article about it. Perhaps can help and be useful for others.

Ispconfig Nginx Directives Laravel

In a typical Laravel installation, the public folder contains all of the publicly accessible files for the application. This includes the index.php file, which...
habibzain
47 sec read

How to Hide Version nginx

Nginx is a popular web server that powers many websites and applications. However, by default, nginx reveals its version number in the HTTP response...
habibzain
1 min read

How to Install SSH in Windows 10/11

SSH (Secure Shell) is a protocol that allows us to securely remote servers and devices over a network. SSH can be used for various...
habibzain
1 min read

Leave a Reply

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

Never miss a story from us, get weekly updates in your inbox