Dear Monks,

What would be the best Perl module to use to read e-mails from a POP3 account?

I am trying to write a Perl script that accesses a POP3 e-mail box and prints out the bodies all of the e-mails that it sees. Ideally I would also like to be able to know certain things about each e-mail such as who the e-mail is from, to, subject line, date, etc.

I am only concerned with the text/plain or text/html portions of the e-mails. I do not need to parse or retrieve e-mail attachments. Also, if the e-mail contains both a text/plain and text/html portion, I would like to be able to access the text/html portion with the HTML tags intact and without all of the encoded e-mail headers.

So far this is what I have:

use Mail::Box::POP3; $url = "pop3://username:password\@server.address.com"; $pop = Mail::Box::POP3->new($url);

What do I write next? How do I start looping through the e-mails? Is this even the correct module to use?

Thanks so much for all of your help.

UPDATE!! a few minutes later!

I was able to access and loop through e-mails in the POP3 account with this simple code:

use Mail::Box::Manager; $mgr = Mail::Box::Manager->new; $pop = $mgr->open(type => 'pop3', username => 'my@email.address', password => 'mypasswordxxxxx', server_name => 'my.mail.server'); foreach $msg ($pop->messages) { $content_type = $msg->contentType; $from_address = $msg->sender->address; $body = $msg->decoded; }

Now, the question is, how do I extract the HTML portion of the body of the e-mail from the rest of the body? For example, here is what the body looks like in its current form:

This is a multi-part message in MIME format. ------=_NextPart_000_0009_01CA7F30.7F3A37C0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit From: sender@email.address Sent: Wednesday, December 16, 2009 9:28 PM To: test@email.address Subject: This is the subject This is the body. ------=_NextPart_000_0009_01CA7F30.7F3A37C0 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable <html> <head> <body> This is the body. </body> </html> ------=_NextPart_000_0009_01CA7F30.7F3A37C0--

So my question is, how do I extract only the part between the <html> and </html> tags?

Thanks so much.


In reply to best module to read e-mail from a POP3 account by keiusui

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.