Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

MIME::Parser and mail from MS Outlook

by neilwatson (Priest)
on Aug 06, 2002 at 17:17 UTC ( [id://188084]=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to learn MIME::Parser. I've written this test script:

#!/usr/bin/perl -w use strict; use warnings; use MIME::Parser; my ($body, $i, $io, $subEntity); my $parser = new MIME::Parser; $parser->ignore_errors(1); $parser->output_to_core(1); my $MIME_entity = $parser->parse(\*STDIN); my $error = ($@ || $parser->last_error); #get from, to and subject headers. my $header = $MIME_entity->head; my $subject = $header->get('Subject'); my $to = $header->get('To'); my $from = $header->get('From'); chomp($subject); chomp($to); chomp($from); #get body if ($MIME_entity->parts > 0){ for ($i=0; $i<$MIME_entity->parts; $i++){ $subEntity = $MIME_entity->parts($i); if (($subEntity->mime_type =~ m/text\/html/) || ($subEntity->m +ime_type =~ m/text\/plain/)){ if ($io = $subEntity->open("r")){ while (defined($_=$io->getline)){ $body .= $_; } $io->close; } } } } else { $body = join "", @{$MIME_entity->body}; } # email report back to sender showing the # parts of original messages open MAIL, ("|/usr/sbin/sendmail -t") || die "Unable to send mail: $!" +; print MAIL "To: $from\n"; print MAIL "From: root\n"; print MAIL "Subject: mime parser test\n\n"; print MAIL "Messege was contructed as follows: \$from: $from \$to: $to \$subject: $subject \$body: $body "; close(MAIL);

The from, to, and subject headers return fine. The body returns properly when I test using Kmail but not the devil's client, Outlook. Can anyone offer some suggestions?

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: MIME::Parser
by Moonie (Friar) on Aug 06, 2002 at 17:31 UTC
      Alas, it did not help. I still cannot get the body of the incomming mail if the sender is Outlook :(

      Neil Watson
      watson-wilson.ca

Re: MIME::Parser
by danboo (Beadle) on Aug 06, 2002 at 17:36 UTC
      I am aware of MS' affection for RTF mail. That is turned off and Outlook is set to plain text.

      Neil Watson
      watson-wilson.ca

Re: MIME::Parser
by trs80 (Priest) on Aug 06, 2002 at 18:16 UTC
    I had a similar problem and corrected by contructing a proper message format using MIME::Lite.
    $msg = MIME::Lite->new( From =>$from, To =>$to, Cc =>'', Subject =>$subject, Data => $body ); $msg->send(); ######################### # if you don't have sendmail on your system # Change how messages are sent ### Do something like this in your 'main': # MIME::Lite->send('smtp', "smtp.myisp.net", Timeout=>60); ### Now this will do the right thing: # $msg->send;## will now use Net::SMTP as shown above
      I'm trying to receive mail and parse it. Not send it.

      Neil Watson
      watson-wilson.ca

        Sorry, maybe I don't fully understand, I am suggesting you replace
        # email report back to sender showing the # parts of original messages open MAIL, ("|/usr/sbin/sendmail -t") || die "Unable to send mail: $!" +; print MAIL "To: $from\n"; print MAIL "From: root\n"; print MAIL "Subject: mime parser test\n\n"; print MAIL "Messege was contructed as follows: \$from: $from \$to: $to \$subject: $subject \$body: $body "; close(MAIL);
        with the MIME::Lite method. The issue I believe is that Outlook can't parse the email because it isn't being told explicitly how to deal with it via the "raw" sendmail type email construction method, and thereby defaults to its internal formatting rules.
Re: MIME::Parser and mail from MS Outlook
by blahblahblah (Priest) on Aug 08, 2002 at 20:07 UTC
    If you haven't already done this, I would suggest dumping a copy of STDIN to a file, once for the mail from outlook and once for the mail from the other client. Something may jump out at you that's obviously not what you expect.

    For example, maybe even though you told outlook to send it as plain text, it's really sending multipart/alternative and you'll have to go another level deep to get the plain text part.

    If you don't see why it's not working after having done this, you could post the outlook mail and maybe someone will see what's wrong with it.

      Yes, I just tried that. I was successful. This seems like a hack to me but it works. Here is the completed script:

      #!/usr/bin/perl -w use strict; use warnings; use MIME::Parser; use MIME::Entity; use MIME::Body; my (@body, $i, $subentity); my $parser = new MIME::Parser; my $to; #contains the message to header my $from; #contains the message from header my $subject; #contains the message subject heaer my $body; #contains the message body $parser->ignore_errors(1); $parser->output_to_core(1); my $entity = $parser->parse(\*STDIN); my $error = ($@ || $parser->last_error); #get email headers my $header = $entity->head; $subject = $header->get('Subject'); $to = $header->get('To'); $from = $header->get('From'); chomp($subject); chomp($to); chomp($from); #get email body if ($entity->parts > 0){ for ($i=0; $i<$entity->parts; $i++){ $subentity = $entity->parts($i); if (($subentity->mime_type =~ m/text\/html/i) || ($subentity-> +mime_type =~ m/text\/plain/i)){ $body = join "", @{$subentity->body}; } #this elsif is needed for Outlook's #nasty multipart/alternative messages elsif ($subentity->mime_type =~ m/multipart\/alternative/i){ $body = join "", @{$subentity->body}; #split html and text parts @body = split /------=_NextPart_\S*\n/, $body; #assign the first part of the message, #hopefully the text part, as the body $body = $body[1]; #remove leading headers from body $body =~ s/^Content-Type.*Content-Transfer-Encoding.*?\n+/ +/is } } } else { $body = join "", @{$entity->body}; } #body may contain html tags. they will be stripped here $body =~ s/(<br>)|(<p>)/\n/gi; #create new lines $body =~ s/<.+\n*.*?>//g; #remove all <> html tages $body =~ s/(\n|\r|(\n\r)|(\r\n)){3,}//g; #remove any extra new lines $body =~ s/\&nbsp;//g; #remove html &nbsp characters #remove trailing whitespace from body $body =~ s/\s*\n+$//s; open MAIL, ("|/usr/sbin/sendmail -t") || die "Unable to send mail: $!" +; print MAIL "To: $from\n"; print MAIL "From: root\n"; print MAIL "Subject: mime parser test\n\n"; print MAIL "Messege was contructed as follows: \$from: $from \$to: $to \$subject: $subject \$body: $body"; close(MAIL);

      Neil Watson
      watson-wilson.ca

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-16 14:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found