邮件服务器中的队列有许多是垃圾邮件的退信,以往清理的时候就会把所有队列中的邮件全部清除掉,这样会把一些正常邮件也清除。

 

在《Postfix 权威指南》里有一个叫 pfdel 的 Perl 小程序,可以用它删除指定邮件地址的邮件(不管是发信人还是收信人的邮件地址),这个虽然方便,但是如果想要清除因为 maildir over quota 或者 Invalid user specified 错误而产生的邮件,还需要修改一下。下面是这四个程序:
=================================================
pfdel.pl 是用来删除队列中指定用户的邮件:
=================================================

程序代码
#!/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;
=================================================
luserdel.pl 是用来删除队列中无效用户的邮件:
=================================================

程序代码
#!/usr/bin/perl -w

#
# luserdel – deletes message containing invalid user from
# Postfix queue.
#
# Usage: luserdel
#

use strict;

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

my $qid = “”;
my $euid = $>;

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 =~ /Invalid user specified/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 invalid user messages found in queue.\n”;
}

exit 0;
=================================================
moqdel.pl 是用来删除队列中邮箱配额已满的用户的邮件:
=================================================

程序代码
#!/usr/bin/perl -w

#
# moqdel – deletes message containing maildir over quota from
# Postfix queue.
#
# Usage: moqdel
#

use strict;

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

my $qid = “”;
my $euid = $>;

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 =~ /maildir over quota/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 maildir over quota messages found in queue.\n”;
}

exit 0;

=================================================
jmoqdel.pl 是删除邮箱配额已满的用户的垃圾邮件箱:
=================================================

程序代码
#!/usr/bin/perl -w

#
# jmoqdel – deletes Junk directories whose maildir over quota from
# Postfix queue.
#
# Usage: jmoqdel
#

use strict;

my $HOME_BASE = “/var/vmail”;
# Change these paths if necessary.
my $LISTQ = “/usr/sbin/postqueue -p”;
my $POSTSUPER = “/usr/sbin/postsuper”;

my $user = “”;
my $domain = “”;
my $email = “”;
my $euid = $>;

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 =~ /maildir over quota/m ) {
($user,$domain,$email) = split(/\n/, $entry, 3);
($user,$domain) = ($email =~ m!\s*(.+)@(.+)\s*!);
`rm $HOME_BASE/$domain/$user/Maildir/.Junk -rf &> /dev/null`;
next unless ($email);
}
}
close(QUEUE);

if (! $email ) {
die “No maildir over quota messages found in queue.\n”;
}

exit 0;

平时用的较多的是第一个程序,已经在邮件服务器上测试通过,这样以后就方便对邮件队列进行管理了。