Pergunta

I have readed what was posted 3 years ago here -> How can I read messages in a Gmail account from Perl?

But i can't open the body .. i've readed Net::IMAP::Simple and Email::Simple; . I'm trying this.. but doesn't works, it prints de from and the subject, but not the body.

use strict;
use warnings;

# required modules
use Net::IMAP::Simple;
use Email::Simple;
use IO::Socket::SSL;

# fill in your details here
my $username = 'email@gmail.com';
my $password = 'pass';
my $mailhost = 'imap.gmail.com';

# Connect
my $imap = Net::IMAP::Simple->new(
    $mailhost,
    port    => 993,
    use_ssl => 1,
) || die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n";

# Log in
if ( !$imap->login( $username, $password ) ) {
    print STDERR "Login failed: " . $imap->errstr . "\n";
    exit(64);
}
# Look in the INBOX
my $nm = $imap->select('INBOX');

# How many messages are there?
my ($unseen, $recent, $num_messages) = $imap->status();
print "unseen: $unseen, recent: $recent, total: $num_messages\n\n";


## Iterate through unseen messages
for ( my $i = 1 ; $i <= $nm ; $i++ ) {
    if ( $imap->seen($i) ) {
        next;
    }
    else {
    my $es = Email::Simple->new( join '', @{ $imap->top($i) } );
    my $text = $es->body;
    printf( "[%03d] %s\n\t%s\n%s", $i, $es->header('From'), $es->header('Subject'),$text);
    }
}


# Disconnect
$imap->quit;

exit;

This print:

[001] <example@example.com>
     test subject

Not the body of the email.

Can anyone solve this???

Thanks in advance.

Foi útil?

Solução

The issue is that you are using:

$imap->top($i)

From the documentation (emphasis mine):

This method accepts a message number as its required parameter. That message will be retrieved from the currently selected folder. On success this method returns a list reference containing the lines of the header. Nothing is returned on failure and the errstr() error handler is set with the error message.

top doesn't return the body of the message. You'll need to use get for that. Something like this:

my $es = Email::Simple->new( join '', @{ $imap->get($i) } );
                                                ^^^
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top