superfrink has asked for the wisdom of the Perl Monks concerning the following question:

Currently spam is being send with the From header set to a bunch of random letters at mydomain. I have a catch-all and I have received over 2200 bounces in the past 12 hours.

I wrote a quick script using Email::Delete to remove the messages from my inbox. The script runs but only process a couple subject lines before hanging. After a few minutes I kill the process with ctrl-c. The code is:
#!/usr/bin/perl -w use strict; use Email::Delete qw(delete_message); my @subject_list = ( '^Delivery Status Notification.*$' , '^Undelivered Mail Returned to Sender$' , '^failure notice$' , '^DELIVERY FAILURE.*$' , '^Undeliverable:.*$' , '^Returned mail: see transcript for deta.*' , ); foreach my $subject (@subject_list) { print $subject , "\n"; delete_message from => '/home/chad/Mailbox', matching => sub { my $message = shift; $message->header('Subject') =~ m/$subject/; }; }
The output is:
$ perl remove-bounce-emails.pl ^Delivery Status Notification.*$ ^Undelivered Mail Returned to Sender$ ^failure notice$ $ perl remove-bounce-emails.pl ^Delivery Status Notification.*$ ^Undelivered Mail Returned to Sender$
Any idea why the program is hanging? top shows the process using next to no CPU time and very little memory.

Replies are listed 'Best First'.
Re: Email::Delete trouble.
by Krambambuli (Curate) on Oct 25, 2007 at 20:42 UTC
    Any idea why the program is hanging? top shows the process using next to no CPU time and very little memory.

    I'd suspect a locking problem; have you made sure that no other mail-related process is trying to access the mailbox while you're deleting the unwanted messages ?

    You haven't said anything about what type of OS and/or Mailbox you have. If things are happening on Linux, you might see which processes concur to access the mailbox using fuser:

    fuser -uv mailbox_filename

    Krambambuli
    ---
    enjoying Mark Jason Dominus' Higher-Order Perl
Re: Email::Delete trouble.
by superfrink (Curate) on Oct 25, 2007 at 19:24 UTC
    I re-worked the script to use one regex and one call to delete_message() and now it runs without hanging. (I also added some more Subject line patterns to filter out.)
    #!/usr/bin/perl -w use strict; use Email::Delete qw(delete_message); my @subject_list = ( '^DELIVERY FAILURE.*$' , '^Delivery Notification: Delivery has failed' , '^Delivery Status Notification.*$' , '^Mail delivery failed: returning message to sender' , '^Mail delivery failed: returning message to sender' , '^Message you sent blocked by our bulk email filter' , '^Returned mail: see transcript for deta.*' , '^Returned mail: see transcript for details' , '^Undeliverable:.*$' , '^Undelivered Mail Returned to Sender$' , '^Undelivered Mail Returned to Sender' , '^failure notice$' , 'failure notice' , ); { local $" = '|'; print "@subject_list" , "\n"; delete_message from => '/home/chad/Mailbox', matching => sub { my $message = shift; local $" = '|'; $message->header('Subject') =~ m/"@subject_list +"/; }; }