Advertisement
Guest User

Untitled

a guest
May 6th, 2017
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.01 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use Net::POP3;
  5. use Date::Parse;
  6.  
  7. my $popserver = 'mail.example.com';
  8. my $username = 'funtimes@example.com';
  9. my $password = 'dogs';
  10.  
  11. my $pop = Net::POP3->new($popserver) or die "Unable to connect to $popserver.\n";
  12. if (!defined $pop->login($username, $password)) {
  13.     die "Unable to login to pop server.\n";
  14. }
  15.  
  16. my $msgnums = $pop->list; # hashref of msgnum => size
  17. foreach my $msgnum (keys %$msgnums) {
  18.     my $msg = $pop->get($msgnum);
  19.     my ($timestamp,$time,$from,$body);
  20.     # parse headers
  21.     while (@$msg) {
  22.         local $_ = shift(@$msg); # shave a line off and parse it
  23.         if (/^Date: (.*)/) {
  24.             $timestamp = $1;
  25.             $time = str2time($timestamp);
  26.         } elsif (/^From: (.*)/) {
  27.             $from = $1;
  28.         } elsif (/^\s$/) { # blank line means end of headers, exit loop
  29.             last;
  30.         }
  31.     }
  32.     # remaining lines in @$msg are the body
  33.     $body = join('',@$msg);
  34.    
  35.     # *** We now have ($time,$from,$body) - DO STUFF ***
  36.    
  37.     # delete message from POP server
  38.     $pop->delete($msgnum);
  39. }
  40.  
  41. $pop->quit;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement