Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- #
- # File /usr/local/bin/pop2smtp.pl
- #
- # Objective
- # Pop email from pop3host & deliver it to spesific account through smtp connection
- # Criteria
- # Get email from spesific date until now
- #
- # Requirement
- # it must search ^Return-Path: <.*?> as a sender address
- # the regex is her
- # while (<>) {
- # if (/^Return-Path: (<.*?>)/)
- # { print "$1\n"; }
- # }
- use Net::POP3;
- use Net::SMTP;
- use Term::ReadKey;
- # cek date
- $CEKDATE='/usr/local/bin/cekdate.sh';
- if (@ARGV < 4)
- {
- print "Usage: $0 username pop3host rcpt\@domain smtphost\n";
- print " eg: pop2smtp.pl 'yudi\@telkom.net' mail.telkom.net yudi\@domain.com smtp.domain.com\n";
- exit;
- }
- ($username, $pop3host, $rcpt, $smtphost) = @ARGV;
- $timeout = 60;
- print "Password at $pop3host: ";
- ReadMode 'noecho';
- $password = ReadLine 0;
- chomp $password;
- ReadMode 'normal';
- print "\n";
- $CEK=0;
- do {
- print "Enter date (eg 1 jan 2011): ";
- $TGL=<STDIN>;
- chomp($TGL);
- # if (system("date --date=\"$TGL\" +%s > /dev/null 2>&1")==0) {
- if (system("date --date=\"$TGL\" +%s > /tmp/SECONDS 2>&1")==0) {
- $CEK=1
- }
- else {
- print "Invalid date !\n"
- }
- }
- until ($CEK==1);
- do {
- print "Flush email after download [Yy/Nn] ? ";
- $_ = <STDIN>;
- chomp;
- } while (! /y|n/i);
- $flush = $_;
- $pop = Net::POP3->new($pop3host)
- or die "Can't open connection to $pop3host : $!\n";
- $smtp = Net::SMTP->new($smtphost,
- Timeout => $timeout,
- debug => 1,
- );
- defined ($pop->login($username, $password))
- or die "Can't authenticate.\n";
- print "Fetching mail from $username at $pop3host newer than $TGL\n. Please, wait a moment ...";
- $msgnums = $pop->list; # hashref of msgnum => size
- $totmsgs = keys (%$msgnums);
- for ($i = 1; $i <= $totmsgs; $i++) {
- $msgsize = $pop->list($i);
- $hdr = $pop->top($i);
- $j = 0;
- $from = "Nobody";
- $subject = "Nosubject";
- while ($$hdr[$j]) {
- $from = $1 if ( ($$hdr[$j]=~/^Return-Path: <(.*)>/) || ($$hdr[$j]=~/^Return-Path: (.*)/));
- $tgl = $1 if ($$hdr[$j] =~ /^Date: (.*)/i);
- $subject = $1 if ($$hdr[$j] =~ /^Subject: (.*)/i);
- $j++;
- }
- # cek tgl
- system("date --date=\"$tgl\" +%s > /tmp/SECONDS.tmp 2>&1");
- # if tgl > dari tgl tertentu dan > today maka get email and sent
- if (system("$CEKDATE")==0) {
- $msg = $pop->get($i);
- $_ = $$msg[0];
- print "$i. Email from: $from, $msgsize bytes, subject: $subject, Date: $tgl, sentto: $rcpt\n";
- $smtp->mail($from);
- $smtp->to($rcpt);
- $smtp->data();
- $smtp->datasend(@$msg);
- $smtp->dataend();
- $pop->delete($i) if ($flush =~ /y/i);
- }
- }
- $smtp->quit;
- $pop->quit;
Advertisement
Add Comment
Please, Sign In to add comment