2

这就是我使用 yo GMail 连接的方式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
4

2 回答 2

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" );
    }
}
于 2013-05-24T10:38:44.707 回答
0

遇到了同样的问题,并能够通过以下方式解决它:

$client->Ignoresizeerrors(1)

于 2017-08-29T18:08:35.033 回答