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

hi there, i'm writing on a script to reads emails(IMAP). I use also Net::IMAP::Simple. i have to read and compare "subject", "From" and "Body" to find needed strings. I stay in the beginning my problem is, i got no values for body. that's the way how i going on in my script:
foreach $msg_id (keys(%$Messages)) { my $MsgContent = $imap->get($msg_id); #my $Inhalt = $imap->get($msg_id); # print $Inhalt; PrintList(@$MsgContent); } sub PrintList { # Assign parameter to a local variable my (@lines) = @_; # Declare local variables my ($from, $line, $subject, $body); # Check each line in the header foreach $line (@lines) { if($line =~ m/^From: (.*)/) { # We found the "From" field, so let's # get what we need $from = $1; $from =~ s/"|<.*>//g; $from = substr($from, 0, 39); } elsif( $line =~ m/^Subject: (.*)/) { # We found the "Subject" field, so let's # get what we need $subject = $1; $subject = substr($subject, 0, 29); } elsif( $line =~ m/^Body: (.*)/ ) { $body = $1; $body = substr($body, 0, 29); #print $body; } # If we have parsed the "From" and "Subject" # field, then we don't need to keep on going. # Let's quit the loop here. last if( defined($subject) && defined($from) && defined($body) ); } # Print the result printf "From: %-40s Subject: %s\nInhalt: %s\n", $from, $subject, $bo +dy; }# end: PrintList()

is there something wrong!? Subject und from are showing from each email but not the body!

Replies are listed 'Best First'.
Re: Using Net::IMAP::Simple to read emails
by bgreenlee (Friar) on Dec 22, 2004 at 14:58 UTC

    The problem is that you're looking for a line that starts with "Body:" and you're not going to find one. Try printing out each line as you process it. After all the header fields, there are two newlines and then the body just starts--there's no tag identifying it.

    You might want to use Email::Simple to handle the parsing of the email message. The example in the Net::IMAP::Simple perldoc uses it.

    -b

      hi bgreenlee, you are right. there is no tyg identifiying for body. i liked to know how to use the Email::Simple to getting any field from the email. The code below bring not any value:
      foreach my $msg( 1 .. $number_of_messages ) { my $email = Email::Simple->new( join '', @{$server->get( $msg +)} ); print $email->header('Subject'), "\n"; }

        I'm not sure what the problem is. I just tried it out myself and it worked fine. Try either stepping through it with the perl debugger (have a look at perldebug and perldebtut if you're not familiar with it), or using Data::Dump to dump out the value of $email. And make sure you use strict and use warnings.

        -b

Re: Using Net::IMAP::Simple to read emails
by sasikumar (Monk) on Dec 22, 2004 at 13:15 UTC
    Hi


    What are u passing here
     PrintList(@$MsgContent);
    it should be as
    PrintList($MsgContent);

    Then
    sub PrintList { my $line = $_[0]; my my @lines; (@lines)=split(/\/n/,$line);

    Then Continue..
    Thanks
    Sasi Kumar
      thanks for your help. i change what you wrote. but now i dont get any value also not for from and subjekt. before i get this values but only not for the body of the email.