设为首页 收藏本站
查看: 951|回复: 0

[经验分享] Postfix 邮件队列管理的几个 Perl 小程序

[复制链接]

尚未签到

发表于 2017-5-19 11:03:53 | 显示全部楼层 |阅读模式
  最近管理邮件系统时发现几个问题,一个是有些用户设置了转发,但是转发地址有问题,经常因为退信而塞爆邮箱(有邮箱限额),之后的邮件都会被塞到等待队列里。还有就是有许多寄到本地虚拟域的信没有对应的用户,按说 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 - 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";
}
 


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";
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-379035-1-1.html 上篇帖子: 使用SOAP::Lite开发Web services(Perl SOAP) 下篇帖子: Note of Learning Perl--Filehandles and File Tests
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表