awarmanf

pop2smtp.pl

Jun 22nd, 2012
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.71 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. #
  4. # File /usr/local/bin/pop2smtp.pl
  5. #
  6. # Objective
  7. #   Pop email from pop3host  & deliver it to spesific account through smtp connection
  8. # Criteria
  9. #   Get email from spesific date until now
  10. #
  11. # Requirement
  12. #   it must search ^Return-Path: <.*?> as a sender address
  13. #   the regex is her
  14. #     while (<>) {
  15. #       if (/^Return-Path: (<.*?>)/)
  16. #       { print "$1\n"; }
  17. #     }
  18.  
  19.  
  20. use Net::POP3;
  21. use Net::SMTP;
  22. use Term::ReadKey;
  23.  
  24. # cek date
  25. $CEKDATE='/usr/local/bin/cekdate.sh';
  26.  
  27. if (@ARGV < 4)
  28. {
  29.   print "Usage: $0 username pop3host rcpt\@domain smtphost\n";
  30.   print "   eg: pop2smtp.pl 'yudi\@telkom.net' mail.telkom.net yudi\@domain.com smtp.domain.com\n";
  31.   exit;
  32. }
  33.  
  34. ($username, $pop3host, $rcpt, $smtphost) = @ARGV;
  35. $timeout  = 60;
  36.  
  37. print "Password at $pop3host: ";
  38. ReadMode 'noecho';
  39. $password = ReadLine 0;
  40. chomp $password;
  41. ReadMode 'normal';
  42. print "\n";
  43.  
  44. $CEK=0;
  45. do {
  46.   print "Enter date (eg 1 jan 2011): ";
  47.   $TGL=<STDIN>;
  48.   chomp($TGL);
  49. #  if (system("date --date=\"$TGL\" +%s > /dev/null 2>&1")==0) {
  50.   if (system("date --date=\"$TGL\" +%s > /tmp/SECONDS 2>&1")==0) {  
  51.     $CEK=1
  52.   }
  53.   else {
  54.     print "Invalid date !\n"
  55.   }
  56. }
  57. until ($CEK==1);
  58.  
  59. do {
  60.   print "Flush email after download [Yy/Nn] ? ";
  61.   $_ = <STDIN>;
  62.   chomp;
  63. } while (! /y|n/i);
  64.  
  65. $flush = $_;
  66.  
  67. $pop = Net::POP3->new($pop3host)
  68.     or die "Can't open connection to $pop3host : $!\n";
  69. $smtp = Net::SMTP->new($smtphost,
  70.                        Timeout => $timeout,
  71.                        debug => 1,
  72.                       );
  73.  
  74. defined ($pop->login($username, $password))
  75.     or die "Can't authenticate.\n";
  76.  
  77. print "Fetching mail from $username at $pop3host newer than $TGL\n. Please, wait a moment ...";
  78. $msgnums = $pop->list; # hashref of msgnum => size
  79. $totmsgs =  keys (%$msgnums);
  80. for ($i = 1; $i <= $totmsgs; $i++) {
  81.   $msgsize = $pop->list($i);
  82.   $hdr = $pop->top($i);
  83.   $j = 0;
  84.   $from = "Nobody";
  85.   $subject = "Nosubject";
  86.   while ($$hdr[$j]) {
  87.     $from = $1 if ( ($$hdr[$j]=~/^Return-Path: <(.*)>/) || ($$hdr[$j]=~/^Return-Path: (.*)/));
  88.     $tgl = $1 if ($$hdr[$j] =~ /^Date: (.*)/i);
  89.     $subject = $1 if ($$hdr[$j] =~ /^Subject: (.*)/i);
  90.     $j++;
  91.   }
  92.   # cek tgl
  93.   system("date --date=\"$tgl\" +%s > /tmp/SECONDS.tmp 2>&1");
  94.   # if tgl > dari tgl tertentu dan > today maka get email and sent
  95.   if (system("$CEKDATE")==0) {
  96.     $msg = $pop->get($i);
  97.     $_ = $$msg[0];
  98.     print "$i. Email from: $from, $msgsize bytes, subject: $subject, Date: $tgl, sentto: $rcpt\n";
  99.     $smtp->mail($from);
  100.     $smtp->to($rcpt);
  101.     $smtp->data();
  102.     $smtp->datasend(@$msg);
  103.     $smtp->dataend();
  104.     $pop->delete($i) if ($flush =~ /y/i);
  105.   }
  106. }
  107. $smtp->quit;
  108. $pop->quit;
Advertisement
Add Comment
Please, Sign In to add comment