2010-11-05 71 views
2

我正在处理一个perl脚本,它将连接到Gmail并下载所有电子邮件。请记住,我现在只专注于连接到Gmail,所以现在只能设置脚本循环浏览一些文件夹。这是我到目前为止有:Perl Mail ::具有IO :: Socket :: SSL帮助的IMAPClient

#!/usr/bin/perl 

use strict; 
use warnings; 
use Mail::IMAPClient; 
use IO::Socket::SSL; 

# Connect to IMAP via SSL and get rid of greeting message 

my $socket = IO::Socket::SSL->new(
PeerAddr => 'imap.gmail.com', 
PeerPort => 993, 
) 
or die "Socket(): [email protected]"; 

my $greeting = <$socket>; 
my ($id, $answer) = split /\s+/, $greeting; 
die "Problems loggin in: $greeting" if $answer ne 'OK'; 

# Build a client attached to the SSL Socket and login 

my $client = Mail::IMAPClient->new(
Socket => $socket, 
User => '[email protected]', 
Password => 'password', 
Port => 993, 
) 
or die "new(): [email protected]"; 

my ($code, $output) = ("",""); 
until($code) { 
$output = $client->_read_line or return undef; 
for my $o (@$output) { 
    $client->_debug("Connect: Recieved this from readline: ".join("/",@$o)."\n"); 
    $client->_reord($client->Count,$o); 
    next unless $o->[Mail::IMAPClient::TYPE] eq "OUTPU"; 
    ($code) = $o->[Mail::IMAPClient::DATA] =~ /^\*\s+(OK|BAD|NO)/i ; 
} 
} 

if($code =~ /BYE|NO /) { 
$client->State("Unconnected"); 
die "IMAP server disconnected"; 
} 

$client->login; 

print "1"; 

$client->select('INBOX'); 
my @mails = ($client->seen(),$client->unseen); 
foreach my $id (@mails) { print "$id\n"; } 
# Terminate the connection with IMAP 

$client->logout(); 

当我运行该脚本,但是,程序只与一个闪烁光标的很长一段时间坐。它返回绝对没有输出或错误。任何人有任何建议?

+0

在哪条线卡住? (调试器可以帮助你,或者穷人在你的代码中散布打印语句的方法。) – Ether 2010-11-05 19:17:31

+0

那么,分散打印命令不会返回任何输出。即使将其放在perl文件的头部也不起作用。 – 2010-11-06 01:43:01

+0

好吧,我仍然没有工作,但我改变了上面的代码。有了这个新的代码,我得到了以下输出:未连接时尝试命令! LastError是:超时等待服务器的数据600s /usr/share/perl5/Mail/IMAPClient.pm 116行 \t Mail :: IMAPClient :: LastError('Mail :: IMAPClient = HASH(0xd57708)','NO not )在/usr/share/perl5/Mail/IMAPClient.pm行1617 \t Mail :: IMAPClient :: _ read_line('Mail :: IMAPClient = HASH(0xd57708)')调用spam.pl行32 Can不会在spam.pl第32行的子例程外面返回。 – 2010-11-06 05:50:37

回答

-1

使用为您的IMAP使用SSL的模块可能更容易。 我已经使用Net::IMAP::Simple::SSLMail::Cclient通过SSL连接到IMAP服务器,虽然我还没有尝试连接到Gmail。

此外,我还没有使用过您正在使用的Mail::IMAPClient,但它似乎支持SSL,并且在文档中多次提到Gmail,因此它应该可以在不必创建自己的SSL套接字的情况下工作。

+0

我会试试这些。 – 2010-11-06 01:43:30

-1

可能值得大喊,我有同样的问题,导致类似的错误,即尝试命令时,未连接! LastError是:超时在/usr/share/perl5/Mail/IMAPClient.pm线等待来自服务器的数据600S 116

我重新启动服务器,一切都OK。

此外,它确实连接之前,该错误,但在某一时刻开始产生它。我不知道发生了什么,但它一定是与服务器/操作系统相关的。