Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

reading body of email not working

by Anonymous Monk
on Jun 01, 2017 at 07:05 UTC ( [id://1191792]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I am trying to read my emails with Perl, using the following code:
use strict; use warnings; # required modules use Net::IMAP::Simple; use Email::Simple; use IO::Socket::SSL; # fill in your details here my $username = 'xxxxx'; my $password = 'yyyy'; my $mailhost = 'pop.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 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) } ); printf( "[%03d] %s\n\t%s\n", $i, $es->header('From'), $es->header( +'Subject'), $es->body ('Body')); } } # Disconnect $imap->quit; exit;

The code works fine, but I cannot see the body of the email (I can see the "From", "Subject" etc), probably the $es->body ('Body') is wrong, but I do not know what the correct would be, can you help me?

Replies are listed 'Best First'.
Re: reading body of email not working -- get not top
by Discipulus (Canon) on Jun 01, 2017 at 08:05 UTC
    Hello again Anonymous Monk

    nice to meet another italian (probably ;=)

    I'm not so skilled in email reading, but me too I receive an empty body. I modified the program a bit with use Data::Dump and then

    printf( "[%03d] %s\n", $i, $es->header('From'), $es->header('Subject') +); print "BODY[".$es->body."]\n\n"; dd $es; # got: BODY[] bless({ body => \"",

    UPDATE: ahah the problem is the top you must use get

    my $es = Email::Simple->new( join '', @{ $imap->get($i) } ); # here!
    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      Thank you very much !
      (I am Greek, but close enough :D :D )
Re: reading body of email not working
by hippo (Bishop) on Jun 01, 2017 at 08:07 UTC

    The documentation of the top() method (which you are using to create $es) in Net::IMAP::Simple is quite clear. It doesn't return the body. Consider using the get() method or similar instead.

Re: reading body of email not working
by Eily (Monsignor) on Jun 01, 2017 at 07:44 UTC

    The format in your printf only contains three variables but your mail body is the fourth. Just add another %s to the format.

      Hm, good observation... I changed it but this line:
      printf( "[%04d] %s\n\t%s\n", $i, $es->header('From'), $es->header('Sub +ject'), $es->body);
      again does not show the body of the email...

        Still only three variables (except the number will be printed with 4 digits instead of three). Your next post is a little more helpful though.

        The documentation for top() in Net::IMAP::Simple states that it returns the lines of the header so it doesn't seem to be the correct way to access the body of the mail.

        As a matter of fact, this command:
        print $es->body;
        does not print anything...
Re: reading body of email not working
by Discipulus (Canon) on Jun 01, 2017 at 07:09 UTC
    Hello,

    I think it is just:  $email->body; and not  $email->body('Body'); as for the synopsis of the module.

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      Same, I only get:
      [608] www-data@gpcr.biocomp.unibo.it (www-data) F4JWB3 | Signal peptide

      so no body still :(

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1191792]
Approved by Discipulus
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-03-29 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found