René Nyffenegger's collection of things on the web | |
René Nyffenegger on Oracle - Most wanted - Feedback
- Follow @renenyffenegger
|
Net::POP3 [Perl] | ||
The following script identifies unwanted eMails and deletes them. Currently, unwanted eMails are those whose subject is the only word 'document';
After it has run, it asks if the identified mails should really be deleted and if answered with y or yes, it really deletes them.
Additionally, it looks for a file called pop-auth in the current directory and if it finds it, it reads the mail server, the username and the password from it.
use warnings; use strict; use Net::POP3; my $subject_width = 50; my $from_width = 80; print "Mail Server: "; my $mailserver = <STDIN>; print "Username: "; my $username = <STDIN>; print "Password: "; my $password = <STDIN>; chomp ($mailserver, $username, $password); my $pop3 = Net::POP3->new($mailserver) or die "Failed to connect to $mailserver"; my $tot_msg = $pop3->login($username,$password) or die "Failed to authenticate $username"; printf("\n There are $tot_msg messages\n\n"); foreach my $msg_id (1 .. $tot_msg) { my $header = $pop3 -> top($msg_id, 0); my ($subject, $from, $status) = analyze_header($header); my $delete = ""; if ($subject eq 'Document') { $delete = 'del'; $pop3->delete($msg_id); # not really deleted until quit is called } printf "[%3d] %-${subject_width}s %-${from_width}s %6s %3s\n", $msg_id, substr($subject,0,$subject_width), substr($from ,0,$from_width ), $status, $delete; } print "Quit and Delete?\n"; my $quit = <STDIN>; chomp $quit; if (lc $quit eq 'y' or lc $quit eq 'yes') { print "quitting and deleting\n"; $pop3 -> quit; # deleted messages are deleted now } sub analyze_header { my $header_array_ref = shift; my $header = join "", @$header_array_ref; my ($subject) = $header =~ /Subject: (.*)/m; my ($from ) = $header =~ /From: (.*)/m; my ($status ) = $header =~ /Status: (.*)/m; if (defined $status) { $status = "Unread" if $status eq 'O'; $status = "Read" if $status eq 'R'; $status = "Read" if $status eq 'RO'; $status = "Ne $status = "-";w" if $status eq 'NEW'; $status = "New" if $status eq 'U'; } else { $status = "-"; } return ($subject, $from, $status); }
The Pop3 protocol is described in RFC 1939
|