LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 06-02-2006, 02:16 PM   #1
rigel_kent
Member
 
Registered: Nov 2004
Posts: 64

Rep: Reputation: 15
Exclamation Extract body message from raw e-mail


Hi,

I'm trying to extract the body message from a raw e-mail message (in the server). I'm using the following test:

Code:
 
use Net::POP3;
use Mail::Internet;

print "Content-type: text/html\n\n";

#I know the $mail_server,$username and $password - I get a good login; no problem with that

    $pop = Net::POP3->new($mail_server);
    $pop = Net::POP3->new($mail_server, Timeout => 60);
 
  if ($pop->login($username, $password) > 0) {
  my $msgnums = $pop->list; # hashref of msgnum => size
  foreach my $msgnum (keys %$msgnums) {
  my $msg = $pop->get($msgnum);
  $rfc2822 = "@$msg";
  my $obj = Mail::Internet->new( [ split /\n/, $rfc2822 ] );
  my $old_body = $obj->body;
  $out = "@$old_body";
  $out =~ s/\n/<br>/gmi;  
  print $out."<br><br>************************************************************<br><br>";
      }
    }
 
    $pop->quit;
 
exit;
The problem is that I get always blank results - but if I print "@$msg", it's all there.

Can someone assist me, please?

Kind regards,

Rigel Kent
 
Old 06-02-2006, 04:46 PM   #2
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 31
I guess the detour is intentional? You dereference $msg into an array, concatenate the array into a string $rfc2822, then split it into an array again, just to pass the array reference to Mail::Internet. In particular, you remove all newline characters from your message along the way.

At least the following works fine:
Code:
  foreach my $msgnum (keys %$msgnums) {
    my $obj = Mail::Internet->new( $pop->get($msgnum)  );
    my $old_body = $obj->body;
    $out = "@$old_body";
    $out =~ s/\n/<br>/gmi;  
    print $out."<br><br>************************************************************<br><br>\n";
  }
It seems like yours doesn't work for the following reason: Have a look at $rfc2822, each line except the very first begins with whitespace. I guess that the lines returned by $pop->get($msgnum) end in "\n ", i.e. newline followed by a space character. So if your string is split at newlines later, those space characters will be shifted to the next line, but then Mail::Internet doesn't recognize the next header.

It also works if you use "split /\n /, $rfc2822", but who knows if the messages returned by Net::POP3 will always look like that? I wonder if this is a bug somewhere.
 
Old 06-03-2006, 06:07 AM   #3
rigel_kent
Member
 
Registered: Nov 2004
Posts: 64

Original Poster
Rep: Reputation: 15
Hi,

You're right. Thanks.

Regards,

Rigel_Kent
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thunderbird prints headers OK but message body corrupt Mq2004 Linux - Software 1 04-10-2006 02:52 PM
Extract information from mail boxes pcwulf Linux - Software 0 06-20-2004 08:04 PM
SMTP logging without including the body of the message jspencereds Linux - Networking 0 04-09-2004 11:33 AM
KMail: execute command depending on message body linuxfond Linux - Newbie 0 01-31-2004 07:30 AM
How to locate and extract a mail from a *nix mailbox? J_Szucs Programming 5 01-15-2003 10:47 AM


All times are GMT -5. The time now is 04:28 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration