use Modern::Perl;
use Mail::POP3Client;
use Email::MIME;
use IO::Socket::SSL;
use Encode;
use HTML::Strip;
# Set output to UTF8
use utf8;
use open ':std', ':encoding(UTF-8)';
#binmode(STDOUT, ":utf8");
#binmode(STDIN, ':encoding(utf8)');
#binmode(STDOUT, ':encoding(utf8)');
#binmode(STDERR, ':encoding(utf8)');
#
# Retrieve emails through POP3
my $pop_user = 'firelll';
my $pop_pass = 't9999';
my $pop_host = 'mail.firelll.com.cn';
# DB info
my $DBcmd = '/usr/bin/mysql';
my $DB = 'updateGame';
my $User = 'gameallupdate';
my $PW = 'gameallupdate!@#$';
my $Tbl = 'version_list';
my $sql = '';
# LOG info
my $LOG = '/tmp/getMail.log';
open(LOGFH, ">>$LOG");
chomp(my $DT = qx/date +%F' '%T/);
print LOGFH "\nrun $DT\n";
# Connect to POP3 sever
# Manually create SSL connection since we can't
# set SSL_verify_mode in Mail::POP3Client
my $socket = IO::Socket::SSL->new( PeerAddr => $pop_host,
PeerPort => 995,
SSL_verify_mode => SSL_VERIFY_NONE,
Proto => 'tcp') || die "No socket!";
my $pop = Mail::POP3Client->new();
$pop->User($pop_user);
$pop->Pass($pop_pass);
$pop->Socket($socket);
$pop->Connect()
or die "Unable to connect to POP3 server: ".$pop->Message()."\n";
# Count number of items in mailbox
my $mailcount = $pop->Count();
#
my %mailid;
if ( $mailcount > 0 ) {
print LOGFH "mailcount:$mailcount\n";
$sql = "select mailid from version_list;";
my $REVT = &commit_sql( $sql );
foreach ( split(/\n/, $REVT) )
{
$mailid{$_}=1;
}
}
# strip html tag to text . so insert mysql table
my $hs = HTML::Strip->new();
# Process each email individually
for (my $i = 1; $i{
my $header = $pop->Head($i); # Gets the email header
my $uni = $pop->Uidl($i); # Gets the unique id
$uni =~ s/\r//g;
chomp($uni);
# filter db already exist mail
if( $mailid{$uni} ) {
next;
}
#say "######### $i: id=$uni\n";
#my $body = $pop->Body($i); # Gets the email body
my $mail = $pop->HeadAndBody($i);
my $parsed = Email::MIME->new($mail);
#my $from = encode('utf8', $parsed->header('From'));
#my $subject = encode('utf8', $parsed->header('Subject'));
my $subject = $parsed->header('Subject');
#say "lqy_header: $header";
#say "lqy_subject: $subject\n";
# filter subject
next if( $subject !~ /^version=/ );
my @parts = $parsed->parts;
for my $part (@parts) {
my $tp = $part->content_type;
#print "content_type: $tp \n";
#next if( $tp =~ /image/) ;
if( $tp =~ /html/) {
my $clean_text = $hs->parse( $part->body_str );
$clean_text =~ s/\r//g;
#print "body: ", $clean_text, "\n";
$sql .= "insert into $Tbl values('','$uni','$subject','$clean_text','$DT');";
} elsif( $tp =~ /plain/ ) {
my $clean_text = $part->body_str ;
$clean_text =~ s/\r//g;
#print "body: ", $clean_text, "\n";
$sql .= "insert into $Tbl values('','$uni','$subject','$clean_text','$DT');";
}
}
} # END for loop
# Close POP connection
$pop->Close();
if ( $sql ) {
my $REV = &commit_sql( $sql );
}
#
sub commit_sql
{
my $sql = shift;
my $REV = qx#echo "$sql" | $DBcmd -u$User -p$PW $DB -N 2>&1#;
if($? eq 0)
{
print LOGFH "[$DT] sucess: $sql \n";
chomp($REV);
return $REV;
} else {
print LOGFH "[$DT] failure: $sql . info:$REV \n";
return -1;
}
}
#
close(LOGFH);