Postfix 邮件队列管理的几个 Perl 小程序

最近管理邮件系统时发现几个问题,一个是有些用户设置了转发,但是转发地址有问题,经常因为退信而塞爆邮箱(有邮箱限额),之后的邮件都会被塞到等待队列里。还有就是有许多寄到本地虚拟域的信没有对应的用户,按说 Postfix应该不会投递这类邮件,但是实际情况是它交给 maildrop 投递,而 maildrop发现没有该用户,报告指定用户非法,这时正确的动作应该是退信,不过可能是我用的版本太低,maildrop没有退信,而是把它放到等待队列里等待下次再试。这样等待队列里经常会有大量的这种邮件。所以,要想办法把这些邮件都清除掉。

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

  • pfdel.pl
  • luserdel.pl
  • moqdel.pl
  • jmoqdel.pl

其中,pfdel.pl 是用来删除队列中指定用户的邮件的,luserdel.pl 是用来删除队列中无效用户的邮件的,moqdel.pl 是用来删除队列中邮箱配额已满的用户的邮件的,jmoqdel.pl 是删除邮箱配额已满的用户的垃圾邮件箱的。

我现在把 luserdel.pl 放到 crontab 里,每天晚上清理一次,终于可以高枕无忧了。

pfdel.pl
#!/usr/bin/perl -w

#

#pfdel-deletesmessagecontainingspecifiedaddressfrom

#Postfixqueue.Matcheseithersenderorrecipientaddress.

#

#Usage:pfdel<email_address>

#

usestrict;

#Changethesepathsifnecessary.

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"Youmustberoottodeletequeuefiles.\n";

}

open(QUEUE,"$LISTQ|")||

die"Can'tgetpipeto$LISTQ:$!\n";

my$entry=<QUEUE>;#skipsingleheaderline

$/="";#Restofqueueentriesprinton

#multiplelines.

while($entry=<QUEUE>){

if($entry=~/$email_addr$/m){

($qid)=split(/\s+/,$entry,2);

$qid=~s/[\*\!]//;

nextunless($qid);

#

#Executepostsuper-dwiththequeueid.

#postsuperprovidesfeedbackwhenitdeletes

#messages.Letitsoutputgothrough.

#

if(system($POSTSUPER,"-d",$qid)!=0){

#Ifpostsuperhasaproblem,bail.

die"Errorexecuting$POSTSUPER:error".

"code".($?/256)."\n";

}

}

}

close(QUEUE);

if(!$qid){

die"Nomessageswiththeaddress<$email_addr>".

"foundinqueue.\n";

}

luserdel.pl

#!/usr/bin/perl -w

#

#luserdel-deletesmessagecontaininginvaliduserfrom

#Postfixqueue.

#

#Usage:luserdel

#

usestrict;

#Changethesepathsifnecessary.

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

my$POSTSUPER="/usr/sbin/postsuper";

my$qid="";

my$euid=$>;

if($euid!=0){

die"Youmustberoottodeletequeuefiles.\n";

}

open(QUEUE,"$LISTQ|")||

die"Can'tgetpipeto$LISTQ:$!\n";

my$entry=<QUEUE>;#skipsingleheaderline

$/="";#Restofqueueentriesprinton

#multiplelines.

while($entry=<QUEUE>){

if($entry=~/Invaliduserspecified/m){

($qid)=split(/\s+/,$entry,2);

$qid=~s/[\*\!]//;

nextunless($qid);

#

#Executepostsuper-dwiththequeueid.

#postsuperprovidesfeedbackwhenitdeletes

#messages.Letitsoutputgothrough.

#

if(system($POSTSUPER,"-d",$qid)!=0){

#Ifpostsuperhasaproblem,bail.

die"Errorexecuting$POSTSUPER:error".

"code".($?/256)."\n";

}

}

}

close(QUEUE);

if(!$qid){

die"Noinvalidusermessagesfoundinqueue.\n";

}

exit 0;
moqdel.pl
#!/usr/bin/perl -w

#

#moqdel-deletesmessagecontainingmaildiroverquotafrom

#Postfixqueue.

#

#Usage:moqdel

#

usestrict;

#Changethesepathsifnecessary.

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

my$POSTSUPER="/usr/sbin/postsuper";

my$qid="";

my$euid=$>;

if($euid!=0){

die"Youmustberoottodeletequeuefiles.\n";

}

open(QUEUE,"$LISTQ|")||

die"Can'tgetpipeto$LISTQ:$!\n";

my$entry=<QUEUE>;#skipsingleheaderline

$/="";#Restofqueueentriesprinton

#multiplelines.

while($entry=<QUEUE>){

if($entry=~/maildiroverquota/m){

($qid)=split(/\s+/,$entry,2);

$qid=~s/[\*\!]//;

nextunless($qid);

#

#Executepostsuper-dwiththequeueid.

#postsuperprovidesfeedbackwhenitdeletes

#messages.Letitsoutputgothrough.

#

if(system($POSTSUPER,"-d",$qid)!=0){

#Ifpostsuperhasaproblem,bail.

die"Errorexecuting$POSTSUPER:error".

"code".($?/256)."\n";

}

}

}

close(QUEUE);

if(!$qid){

die"Nomaildiroverquotamessagesfoundinqueue.\n";

}

exit0;

jmoqdel.pl

#!/usr/bin/perl -w

#

#jmoqdel-deletesJunkdirectorieswhosemaildiroverquotafrom

#Postfixqueue.

#

#Usage:jmoqdel

#

usestrict;

my$HOME_BASE="/var/vmail";

#Changethesepathsifnecessary.

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

my$POSTSUPER="/usr/sbin/postsuper";

my$user="";

my$domain="";

my$email="";

my$euid=$>;

if($euid!=0){

die"Youmustberoottodeletequeuefiles.\n";

}

open(QUEUE,"$LISTQ|")||

die"Can'tgetpipeto$LISTQ:$!\n";

my$entry=<QUEUE>;#skipsingleheaderline

$/="";#Restofqueueentriesprinton

#multiplelines.

while($entry=<QUEUE>){

if($entry=~/maildiroverquota/m){

($user,$domain,$email)=split(/\n/,$entry,3);

($user,$domain)=($email=~m!\s*(.+)@(.+)\s*!);

`rm$HOME_BASE/$domain/$user/Maildir/.Junk-rf&>/dev/null`;

nextunless($email);

}

}

close(QUEUE);

if(!$email){

die"Nomaildiroverquotamessagesfoundinqueue.\n";

}

相关推荐