Perl Mail ::带有IO :: Socket :: SSL帮助的IMAPClient

时间:2010-11-05 18:40:40

标签: perl imap

我正在处理将连接到Gmail并下载所有电子邮件的perl脚本。请记住,我现在只专注于连接到Gmail,因此脚本目前只设置为循环访问某些文件夹。这就是我到目前为止所做的:

    #!/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(): $@";

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 => 'mail@gmail.com',
 Password => 'password',
 Port  => 993,
 )
 or die "new(): $@";

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();

但是,当我运行脚本时,程序只会长时间闪烁光标。它绝对没有输出或错误。有人有什么建议吗?

2 个答案:

答案 0 :(得分:-1)

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

此外,我还没有使用您正在使用的Mail::IMAPClient,但它似乎支持SSL,并且在文档中多次提及Gmail,因此无需创建自己的SSL套接字即可使用

答案 1 :(得分:-1)

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

我重新启动了服务器,一切正常。

它也确实在该错误之前连接,但在某些时候开始产生它。我不知道发生了什么,但它必须与服务器/操作系统有关。

相关问题