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

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.

Replies are listed 'Best First'.
Re: best module to read e-mail from a POP3 account
by gmargo (Hermit) on Dec 21, 2009 at 20:46 UTC

    In the previous recent thread parsing and printing e-mail, keszler suggested the Email::MIME module.

    Here's a test program that uses Email::MIME to pull apart a full message. You should be able to figure out how to isolate just the parts you want.

    #!/usr/bin/perl -w use strict; use warnings; use Email::MIME; my $body = join("",(<DATA>)); my $email = Email::MIME->new($body); print "===== Top Header =====\n"; print "From: ".($email->header("From"))."\n"; print "Date: ".($email->header("Date"))."\n"; print "Subject: ".($email->header("Subject"))."\n"; print "\n===== Top Body =====\n"; print "".($email->body())."\n"; my @parts = $email->parts(); print "There are ".@parts." parts in this message.\n"; my $p = 0; foreach my $part (@parts) { $p++; print "\n===== Part $p Header =====\n"; print "Content-Type: ".($part->header("Content-Type"))."\n"; print "Content-Transfer-Encoding: ".($part->header("Content-Transf +er-Encoding"))."\n"; print "\n===== Part $p Body =====\n"; print "".($part->body())."\n"; } __DATA__ From: homer@simpson.net To: marge@simpson.net Subject: Bart and Lisa Date: Thu, 17 Sep 2009 12:17:23 -0700 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0009_01CA7F30.7F3A37C0" Mime-Version: 1.0 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--

    Output from the program:

    ===== Top Header ===== From: homer@simpson.net Date: Thu, 17 Sep 2009 12:17:23 -0700 Subject: Bart and Lisa ===== Top Body ===== This is a multi-part message in MIME format. There are 2 parts in this message. ===== Part 1 Header ===== Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit ===== Part 1 Body ===== 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. ===== Part 2 Header ===== Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable ===== Part 2 Body ===== <html> <head> <body> This is the body. </body> </html>
Re: best module to read e-mail from a POP3 account
by zentara (Cardinal) on Dec 22, 2009 at 13:40 UTC