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

So I want to end my script, so i add function to read e-mail. No i can write e-mail's id and than i'll see e-mail's subject. That script will ask us "Read e-mail?", than we'll see e-mail.But!We see many unnecessarys information....;( i want to see only message, but i don't know how to write this. Documentation of Net:POP3 did't help;(
#!/usr/bin/perl use warnings; use strict; use Net::POP3; my $time = scalar localtime; my $pop3 = "pop3.poczta.onet.pl"; my $login = "haptor\@op.pl"; my $pass = "***************"; my $pop = Net::POP3->new($pop3); my $res = $pop->login($login, $pass); if ($res){ print"On $time e-mail status inbox: $res post \n"; } else { print "Couldn't connect $!\n"; exit; } my $msg; my $msgnum; do{ print "Write id(number) email to read subject:\n"; chomp($msgnum = <STDIN>); } while($msgnum > $res); $msg = $pop->get($msgnum); for my $line ( @$msg ) { if( $line =~ /^Subject:(.*)/ ) { print "Subject is $1\n"; last } } print "Read e-mail? [y/n]\n"; chomp(my $ans = <STDIN>); if ($ans =~ /y/i){ print "Messsage is:\n @$msg \n"; #problem } elsif ($ans =~ /n/i){ exit; } else { print "Bad!"; exit; } $pop->quit();
I am to newbie to unknot this problem... i think that's array ref. or somethink...:(

Replies are listed 'Best First'.
Re: Almost success
by BUU (Prior) on Dec 04, 2005 at 01:53 UTC
    If by "But!We see many unnecessarys information....;( i want to see only message, but i don't know how to write this" you mean you don't want to see the headers preceding the content body, you simply need to skip them when output the message. Headers in smtpd are delimited by an empty line or two newlines in a row. For example:
    while( my $line = shift @$msg ) { last unless length $line; } for( @$msg ) { print "Body: $_"; }
    The first while loop removes the first element from the array each time it loops. If the element is empty, signifying we have reached the end of the headers, we exit the loop. The next loop can simply print the remainder.

    I should note that you are unlikely to acheive good results if you have some sort of multipart message, you might want to consider using an actual parser from CPAN.
      Thank you very very much^^ now my script looks nice^^ Here is source:
      #!/usr/bin/perl use warnings; use strict; use Net::POP3; my $time = scalar localtime; my $pop3 = "pop3.poczta.onet.pl"; my $login = "haptor\@op.pl"; my $pass = "******************"; my $pop = Net::POP3->new($pop3); my $res = $pop->login($login, $pass); if ($res){ print"On $time e-mail status inbox: $res post \n"; } else { print "Couldn't connect $!\n"; exit; } my $msg; my $msgnum; do{ print "Write id(number) email to read subject:\n"; chomp($msgnum = <STDIN>); } while($msgnum > $res); $msg = $pop->get($msgnum); for my $line ( @$msg ) { if( $line =~ /^Subject:(.*)/ ) { print "Subject is $1\n"; last } } print "Read e-mail? [y/n]\n"; chomp(my $ans = <STDIN>); if ($ans =~ /y/i){ while( my $line = shift @$msg ) { last while $line =~ /^Status:/; } my $nline = 0; for( @$msg ) { $nline += 1; print "$nline> $_"; } } elsif ($ans =~ /n/i){ exit; } else { print "Bad!"; exit; } $pop->quit();
      If somebody use this scripts i'll be very happy^^ i love Perl;p