【发布时间】:2012-04-07 16:20:10
【问题描述】:
我编写了一个脚本来使用正则表达式和 .如果匹配,它会将附件下载到输出位置。
我使用的 Perl 模块是 Mail::POP3Client(用于获取连接)和 MIME::Parser(用于解析附件)。我正在使用的电子邮件服务器是 Gmail。
然而,奇怪的事情发生了。它在我的电脑上工作,但在另一台电脑上失败!您可以下载代码并用您的个人信息替换那些 ARGV[i] 并尝试运行该代码;
如果返回的邮件数不是'-1',则表示连接成功。
很奇怪,这段代码在我的电脑上工作,但在另一台电脑上却失败了(总是显示 -1 电子邮件)!我在想,是因为 Perl 模块安装错误还是因为邮件被防火墙阻止了?
错误信息是: 有 [-1] 封电子邮件!
代码是:
#!/usr/bin/perl -w
use strict;
use warnings;
use Mail::POP3Client;
use MIME::Parser;
use MIME::Entity;
use File::Copy;
use File::Glob ':glob';
my ($DAY,$MONTH,$YEAR);
my $host = 'pop.gmail.com';
my $user = $ARGV[0];
my $passwd = $ARGV[1];
my $sender =$ARGV[2];
my $outputloc=$ARGV[3];
my $subject=$ARGV[4];
my $attachedfile=$ARGV[5];
my $outfile=$ARGV[6];
my $today=return_time();
my $flag=0;
$attachedfile =~s/date/$today/g;
$outfile =~s/date/$today/g;
my $client = new Mail::POP3Client(
USER => $user,
PASSWORD => $passwd,
HOST => "pop.gmail.com",
PORT => 995,
USESSL => 'true',
);
my $parser = MIME::Parser->new;
$parser->output_dir("T:/dailyfiles/TEMP");
my $mgrnum = $client->Count;
print "There are [$mgrnum] emails!\n";
for ( my $i = 1 ; $i <= $mgrnum ; $i++ ) {
my $headandbody = $client->HeadAndBody($i);
my $entity = $parser->parse_data($headandbody);
$parser->decode_headers(1);
my $Subject=$entity->head->get('Subject');
my $From=$entity->head->get('From');
my $To=$entity->head->get('To');
my $Date=$entity->head->get('Date');
my $MIME_type=$entity->mime_type;
print "From = ",$From;
print "To = ",$To;
print "Cc = ",$entity->head->get('Cc'),"\n";
print "Subject = ",$Subject;
print "Date = ",$Date;
print "MIME type = ",$entity->mime_type,"\n";
print "Parts = ",scalar $entity->parts,"\n";
print "=========================================================\n";
exit if ( (scalar $entity->parts) == 1 );
chomp($Subject);
chomp($From);
if($Subject eq $subject && $From eq $sender) {
chdir "T:/dailyfiles/TEMP";
my @list = bsd_glob('*.txt');
my @list2 = bsd_glob('*.html');
unlink(@list,@list2);
my $dir="T:/dailyfiles/TEMP/";
opendir(DIR,$dir) or die$!;
while(defined(my $file=readdir DIR)){
my $oldfile=$file;
if($file =~/$attachedfile/){
$flag=1;
print "Original Attachment: $oldfile\n";
print "Renamed Attachment: ",$outfile,"\n";
if (-e $outfile) {
warn "can't rename $oldfile to $outfile: $file exists ";
} elsif (rename $oldfile, $outfile) {
} else {
warn "rename $oldfile to $outfile failed:$! ";
}
## copy and move files
move("T:/dailyfiles/TEMP/$outfile",$outputloc.$outfile);
print "STATUS: Required email arrival. Expected attachment forwarded to $outputloc.\n";
print "=========================================================\n";
}
}
if($flag==0){
print "STATUS: Required email arrival. However, attachment is disqualified.\n";
}
}
else{
print "STATUS: Required email not yet come. Try later.\n";
}
}
delete_dir();
##subroutines go here
sub return_time{
($DAY,$MONTH,$YEAR)=(gmtime)[3,4,5];
$today=sprintf("%04d%02d%02d",$YEAR+1900,$MONTH+1,$DAY);
return $today;
}
sub delete_dir{
my $dir="T:/dailyfiles/TEMP/";
opendir(DIR,$dir) or die$!;
while(defined(my $file=readdir DIR)){
unlink($dir.$file);
}
}
【问题讨论】:
标签: regex perl email gmail firewall