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

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

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


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

 

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

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

pfdel.pl
  1. #!/usr/bin/perl -w
  2. #
  3. # pfdel - deletes message containing specified address from
  4. # Postfix queue. Matches either sender or recipient address.
  5. #
  6. # Usage: pfdel <email_address>
  7. #
  8.  
  9. use strict;
  10.  
  11. # Change these paths if necessary.
  12. my $LISTQ = "/usr/sbin/postqueue -p";
  13. my $POSTSUPER = "/usr/sbin/postsuper";
  14.  
  15. my $email_addr = "";
  16. my $qid = "";
  17. my $euid = $>;
  18.  
  19. if ( @ARGV !=  1 ) {
  20.         die "Usage: pfdel <email_address>\n";
  21. } else {
  22.         $email_addr = $ARGV[0];
  23. }
  24.  
  25. if ( $euid != 0 ) {
  26.         die "You must be root to delete queue files.\n";
  27. }
  28.  
  29.  
  30. open(QUEUE, "$LISTQ |") ||
  31.   die "Can't get pipe to $LISTQ: $!\n";
  32.  
  33. my $entry = <QUEUE>;    # skip single header line
  34. $/ = "";                # Rest of queue entries print on
  35.                         # multiple lines.
  36. while ( $entry = <QUEUE> ) {
  37.         if ( $entry =~ / $email_addr$/m ) {
  38.                 ($qid) = split(/\s+/, $entry, 2);
  39.                 $qid =~ s/[\*\!]//;
  40.                 next unless ($qid);
  41.  
  42.                 #
  43.                 # Execute postsuper -d with the queue id.
  44.                 # postsuper provides feedback when it deletes
  45.                 # messages. Let its output go through.
  46.                 #
  47.                 if ( system($POSTSUPER, "-d", $qid) != 0 ) {
  48.                         # If postsuper has a problem, bail.
  49.                         die "Error executing $POSTSUPER: error " .
  50.                            "code "($?/256) . "\n";
  51.                 }
  52.         }
  53. }
  54. close(QUEUE);
  55.  
  56. if (! $qid ) {
  57.         die "No messages with the address <$email_addr> " .
  58.           "found in queue.\n";
  59. }
  60.  
  61. exit 0;
luserdel.pl
  1. #!/usr/bin/perl -w
  2. #
  3. # luserdel - deletes message containing invalid user from
  4. # Postfix queue.
  5. #
  6. # Usage: luserdel
  7. #
  8.  
  9. use strict;
  10.  
  11. # Change these paths if necessary.
  12. my $LISTQ = "/usr/sbin/postqueue -p";
  13. my $POSTSUPER = "/usr/sbin/postsuper";
  14.  
  15. my $qid = "";
  16. my $euid = $>;
  17.  
  18. if ( $euid != 0 ) {
  19.         die "You must be root to delete queue files.\n";
  20. }
  21.  
  22.  
  23. open(QUEUE, "$LISTQ |") ||
  24.   die "Can't get pipe to $LISTQ: $!\n";
  25.  
  26. my $entry = <QUEUE>;    # skip single header line
  27. $/ = "";                # Rest of queue entries print on
  28.                         # multiple lines.
  29. while ( $entry = <QUEUE> ) {
  30.         if ( $entry =~ /Invalid user specified/m ) {
  31.                 ($qid) = split(/\s+/, $entry, 2);
  32.                 $qid =~ s/[\*\!]//;
  33.                 next unless ($qid);
  34.  
  35.                 #
  36.                 # Execute postsuper -d with the queue id.
  37.                 # postsuper provides feedback when it deletes
  38.                 # messages. Let its output go through.
  39.                 #
  40.                 if ( system($POSTSUPER, "-d", $qid) != 0 ) {
  41.                         # If postsuper has a problem, bail.
  42.                         die "Error executing $POSTSUPER: error " .
  43.                            "code "($?/256) . "\n";
  44.                 }
  45.         }
  46. }
  47. close(QUEUE);
  48.  
  49. if (! $qid ) {
  50.         die "No invalid user messages found in queue.\n";
  51. }
  52.  
  53. exit 0;
moqdel.pl
  1. #!/usr/bin/perl -w
  2. #
  3. # moqdel - deletes message containing maildir over quota from
  4. # Postfix queue.
  5. #
  6. # Usage: moqdel
  7. #
  8.  
  9. use strict;
  10.  
  11. # Change these paths if necessary.
  12. my $LISTQ = "/usr/sbin/postqueue -p";
  13. my $POSTSUPER = "/usr/sbin/postsuper";
  14.  
  15. my $qid = "";
  16. my $euid = $>;
  17.  
  18. if ( $euid != 0 ) {
  19.         die "You must be root to delete queue files.\n";
  20. }
  21.  
  22.  
  23. open(QUEUE, "$LISTQ |") ||
  24.   die "Can't get pipe to $LISTQ: $!\n";
  25.  
  26. my $entry = <QUEUE>;    # skip single header line
  27. $/ = "";                # Rest of queue entries print on
  28.                         # multiple lines.
  29. while ( $entry = <QUEUE> ) {
  30.         if ( $entry =~ /maildir over quota/m ) {
  31.                 ($qid) = split(/\s+/, $entry, 2);
  32.                 $qid =~ s/[\*\!]//;
  33.                 next unless ($qid);
  34.  
  35.                 #
  36.                 # Execute postsuper -d with the queue id.
  37.                 # postsuper provides feedback when it deletes
  38.                 # messages. Let its output go through.
  39.                 #
  40.                 if ( system($POSTSUPER, "-d", $qid) != 0 ) {
  41.                         # If postsuper has a problem, bail.
  42.                         die "Error executing $POSTSUPER: error " .
  43.                            "code "($?/256) . "\n";
  44.                 }
  45.         }
  46. }
  47. close(QUEUE);
  48.  
  49. if (! $qid ) {
  50.         die "No maildir over quota messages found in queue.\n";
  51. }
  52.  
  53. exit 0;
jmoqdel.pl
  1. #!/usr/bin/perl -w
  2. #
  3. # jmoqdel - deletes Junk directories whose maildir over quota from
  4. # Postfix queue.
  5. #
  6. # Usage: jmoqdel
  7. #
  8.  
  9. use strict;
  10.  
  11. my $HOME_BASE = "/var/vmail";
  12. # Change these paths if necessary.
  13. my $LISTQ = "/usr/sbin/postqueue -p";
  14. my $POSTSUPER = "/usr/sbin/postsuper";
  15.  
  16. my $user = "";
  17. my $domain = "";
  18. my $email = "";
  19. my $euid = $>;
  20.  
  21. if ( $euid != 0 ) {
  22.         die "You must be root to delete queue files.\n";
  23. }
  24.  
  25.  
  26. open(QUEUE, "$LISTQ |") ||
  27.   die "Can't get pipe to $LISTQ: $!\n";
  28.  
  29. my $entry = <QUEUE>;    # skip single header line
  30. $/ = "";                # Rest of queue entries print on
  31.                         # multiple lines.
  32. while ( $entry = <QUEUE> ) {
  33.         if ( $entry =~ /maildir over quota/m ) {
  34.                 ($user,$domain,$email) = split(/\n/, $entry, 3);
  35.                 ($user,$domain) = ($email =~ m!\s*(.+)@(.+)\s*!);
  36.                 `rm $HOME_BASE/$domain/$user/Maildir/.Junk -rf &> /dev/null`;
  37.                 next unless ($email);
  38.         }
  39. }
  40. close(QUEUE);
  41.  
  42. if (! $email ) {
  43.         die "No maildir over quota messages found in queue.\n";
  44. }
  45.  
  46. exit 0;

标签: Linux, Mail

« 上一篇 | 下一篇 »

只显示10条记录相关文章

发表评论

评论 (必须):