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

Hi Monks,

I am trying to write a simple Perl script that loops through all of my e-mails and prints the body of each e-mail to my web browser. Here is what I have so far:

use Mail::POP3Client; $pop = new Mail::POP3Client( USER => "my\@email.address", PASSWORD => "passwordxxxxx", HOST => "mail.mydomain.com" ); for($i = 1; $i <= $pop->Count(); $i++) { $body = $pop->Body($i); print "<p>$body<p><hr>"; }

This code works perfect for plain text e-mails.

However, with html e-mails, the script prints out all sorts of ugly headers. In addition, if the e-mail contains both an html and text version of the body, then the script outputs both the html and text versions (I only need one version, preferably the html version).

Does anyone know of a good way to parse HTML e-mails so that they can be printed out to the web browser?

Replies are listed 'Best First'.
Re: parsing and printing e-mail
by keszler (Priest) on Dec 17, 2009 at 19:41 UTC
    MIME::Email - something like
    use strict; use warnings; use Email::MIME; use Mail::POP3Client; my $pop = new Mail::POP3Client( USER => "my\@email.address", PASSWORD => "passwordxxxxx", HOST => "mail.mydomain.com" ); for( my $i = 1; $i <= $pop->Count(); $i++) { my $body = $pop->Body($i); my $parsed = Email::MIME->new($body); my $decoded = $parsed->body; print "<p>$decoded<p><hr>"; }
      I already tried this. Decoding the body using Email::MIME still leaves the ugly headers intact. However, thanks for your response.

        Could you post an example of the "ugly headers"? That may provide a clue. I suspect a Content-Type problem.

Re: parsing and printing e-mail
by zwon (Abbot) on Dec 17, 2009 at 19:58 UTC