2012-03-23 14 views
0

正規表現とを使って指定された件名の指定ユーザーからのメールの到着を確認するスクリプトを作成しました。一致する場合は、添付ファイルを出力場所にダウンロードします。PerlがGmailを読んで添付ファイルを解析しました - パソコンでは成功しましたが別のパソコンでは失敗しました

私が使用したPerlモジュールはMail :: POP3Client(接続を取得する)とMIME :: Parser(添付ファイルを解析する)です。私が取り組んでいる電子メールサーバーはGmailです。

しかし、奇妙なことが起こります。私のPCで動作しますが、別のPCで動作しません!コードをダウンロードしてARGV [i]をあなたの個人情報に置き換えて、そのコードを実行してみてください。

返された電子メールの数が「-1」でない場合、接続に成功したことを意味します。

このコードは自分のPCで動作しますが、別のPC(常に-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); 
} 
} 

答えて

0

それはエラーを追跡することをお勧めします:

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 
0

は、使用しているモジュールのバージョンを確認してください。

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; 
関連する問題