这就是我使用Mail::IMAPClient
sub connectGMail
{
my $socket = IO::Socket::SSL->new
(
PeerAddr => 'imap.gmail.com',
PeerPort => 993,
SSL_verify_mode => SSL_VERIFY_NONE
)
or die "socket(): $@";
my $client = Mail::IMAPClient->new
(
User => 'whateverUser',
Password => 'aG00dP455w0rd'
Socket => $socket
)
or die "Cannot connect ($@)\n";
return $client;
}
然后我将此客户端对象传递给processEmails
子例程
sub processEmails
{
my $client = shift;
if ($client->IsAuthenticated())
{
$client->select('Pruebas');
my @messages = $client->messages();
my $mime_parser = getMIMEParser();
my $filer = MIME::Parser::FileInto->new();
mkdir(EXTRACT_DIR);
for(@messages)
{
my $msg_body = $client->message_string($_);
my $msg_subject = Encode::decode('MIME-Header', $client->subject($_));
if ($msg_body)
{
my $dir = EXTRACT_DIR . "/" . $msg_subject;
$filer->init($dir);
mkdir($dir);
$mime_parser->filer($filer);
my $ok = parseEmail($mime_parser, $msg_body);
if ($ok)
{
print(STDERR "Email \"$msg_subject\" parsed OK\n");
}
}
else
{
print(STDERR "No body for message with subject $msg_subject\n");
}
}
}
else
{
print(STDERR "Something nasty happened\n");
}
}
问题是$msg_body
(使用message_string
进行了检索)对于所有邮件都是空的,但$msg_subject
是正确的,这意味着连接正常。这是输出:
No body for message with subject íQuÚ pasa!
No body for message with subject Prueba con formateo HTML
答案 0 :(得分:0)
一般来说,如果您尝试包含可运行的代码示例,我认为这将非常有用。
我稍微改变了你的代码,对我来说它完全正常。我的代码库中有非常相似的代码。
use Mail::IMAPClient;
use MIME::Parser::Filer;
my $client = Mail::IMAPClient->new(
Server => 'imap.gmail.com',
User => 'test@gmail.com',
Password => 'test-pass',
Ssl => 1,
Uid => 1,
) or die "Cannot connect ($@)\n";
$client->select('INBOX');
my @messages = $client->messages();
my $filer = MIME::Parser::FileInto->new();
print "There are " . scalar @messages . " messages waiting.\n";
for (@messages) {
print "Processing...\n";
my $msg_body = $client->message_string($_);
my $msg_subject = Encode::decode( 'MIME-Header', $client->subject($_) );
if ($msg_body) {
print "$msg_body\n";
print( STDERR "Email \"$msg_subject\" parsed OK\n" );
}
else {
print( STDERR "No body for message with subject $msg_subject\n" );
}
}
答案 1 :(得分:0)
遇到同样的问题并且能够通过以下方式解决:
$client->Ignoresizeerrors(1)