How can I read the body of a messages in a Gmail account from Perl? -
i have readed posted 3 years ago here -> how can read messages in gmail account perl?
but can't open body .. i've readed net::imap::simple , email::simple; . i'm trying this.. doesn't works, prints de , subject, not body.
use strict; use warnings; # required modules use net::imap::simple; use email::simple; use io::socket::ssl; # fill in details here $username = 'email@gmail.com'; $password = 'pass'; $mailhost = 'imap.gmail.com'; # connect $imap = net::imap::simple->new( $mailhost, port => 993, use_ssl => 1, ) || die "unable connect imap: $net::imap::simple::errstr\n"; # log in if ( !$imap->login( $username, $password ) ) { print stderr "login failed: " . $imap->errstr . "\n"; exit(64); } # in the inbox $nm = $imap->select('inbox'); # how many messages there? ($unseen, $recent, $num_messages) = $imap->status(); print "unseen: $unseen, recent: $recent, total: $num_messages\n\n"; ## iterate through unseen messages ( $i = 1 ; $i <= $nm ; $i++ ) { if ( $imap->seen($i) ) { next; } else { $es = email::simple->new( join '', @{ $imap->top($i) } ); $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 body of email.
can solve this???
thanks in advance.
the issue using:
$imap->top($i)
from the documentation (emphasis mine):
this method accepts message number required parameter. message retrieved selected folder. on success method returns list reference containing lines of header. nothing returned on failure , errstr() error handler set error message.
top
doesn't return body of message. you'll need use get
that. this:
my $es = email::simple->new( join '', @{ $imap->get($i) } ); ^^^
Comments
Post a Comment