【问题标题】:Perl read Gmail and parse the attachments- successful on my PC but failed on another onePerl 读取 Gmail 并解析附件——在我的电脑上成功,但在另一台电脑上失败
【发布时间】: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


    【解决方案1】:

    阅读 Mail::POP3Client 文档,我看到您应该在创建对象后检查您的 POP3 连接。您收到的错误表明发生了连接错误:

    new 在所有情况下都返回一个有效的 Mail::POP3Client 对象。测试 连接失败,您将需要检查消息数量: -1 表示连接错误。这可能会在将来的某个时候更改为在错误时返回 undef,设置 $!作为副作用。 此更改不会在任何 2.x 版本中发生。

    你可以这样做:

    my $client = new Mail::POP3Client(
      USER     => $user,
      PASSWORD => $passwd,
      HOST     => "pop.gmail.com",
      PORT     => 995,
      USESSL   => 'true',
    );
    die "Can't connect to the server" if $client->Count == -1;
    

    【讨论】:

      【解决方案2】:

      检查您正在使用的模块的版本。

      【讨论】:

        【解决方案3】:

        跟踪错误是个好主意:

        my $client = new Mail::POP3Client(
            USER     => $user,
            PASSWORD => $passwd,
            HOST     => "pop.gmail.com",
            PORT     => 995,
            USESSL   => 'true',
        ) || die $!;
        #  ^^^^^^^^^ insert this and you will show connection error
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-12-20
          • 2021-11-01
          • 2023-04-07
          • 1970-01-01
          • 2012-08-04
          • 2018-06-20
          • 1970-01-01
          相关资源
          最近更新 更多