I had to write a program to read emails through POP3. Based on the searches I've made and the documentation I've read, it was relatively easy to do using Mail::POP3Client. There are enough examples to get a basic program that will retrieve emails written very quickly. However, I was surprised that I couldn't easily find a more detailed program. For example, I soon learned (the hard way) about message encodings (Base64 and Quoted-Printable). I found posts here that directed people to MIME::QuotedPrint and MIME::Base64 libraries for decoding the email bodies. It was easy enough to check the header for encoding entries:

# Process header for (split /^/, $header) { if ( /^From:\s+<(.*?)>/i ) { $from = $1 }; if ( /^Subject:\s+(.*)/i ) { $subject = $1 }; } # Decode body if encoded if ( $header =~ /Content-Transfer-Encoding:\s+base64/is ) { $body = decode_base64($body); } elsif ( $header =~ /Content-Transfer-Encoding:\s+quoted-printable/is ) { $body = decode_qp($body); }

But I was surprised this was a "manual" process left up to the user. Then I discovered some characters in the body were wrong. The emails are utf8 encoded and seemed to be readable 99.9% of the time, but every so often, I'd see a character get messed up. Things like a single-right-quote wouldn't convert properly. I searched again and found Encoding::FixLatin which did the trick. So yes, I got it all working, but it seemed very inelegant.

I happened to stumble on a bit of code while looking for more information on Perl and utf8 that used Email::MIME to process emails. Most examples using Email::MIME I've seen are for encoding (i.e., sending) emails. I'm surprised at the lack of search results for email reading examples using Email::MIME.

I don't like to ask for help for things that may be obvious by Googling. However, at this point I just want some verification and suggestions for improvement. I'm posting up a sample Perl script that will read email from a SSL POP3 connection. What I'd like to know is, am I doing it "right?" Is this the best way to accomplish this?

#!/usr/bin/perl use Modern::Perl; use Mail::POP3Client; use Email::MIME; use IO::Socket::SSL; use Encode; # Set output to UTF8 use utf8; binmode(STDOUT, ":utf8"); # Retrieve emails through POP3 my $pop_user = '********'; my $pop_pass = '********'; my $pop_host = 'mail.********.com'; # Connect to POP3 sever # Manually create SSL connection since we can't # set SSL_verify_mode in Mail::POP3Client my $socket = IO::Socket::SSL->new( PeerAddr => $pop_host, PeerPort => 995, SSL_verify_mode => SSL_VERIFY_NONE, Proto => 'tcp') || die "N +o socket!"; my $pop = Mail::POP3Client->new(); $pop->User($pop_user); $pop->Pass($pop_pass); $pop->Socket($socket); $pop->Connect() or die "Unable to connect to POP3 server: ".$pop->Message()."\n"; # Count number of items in mailbox my $mailcount = $pop->Count(); # Process each email individually for (my $i = 1; $i <= $mailcount ; $i++) { my $header = $pop->Head($i); # Gets the email header #my $uni = $pop->Uidl($i); # Gets the unique id #my $body = $pop->Body($i); # Gets the email body my $mail = $pop->HeadAndBody($i); my $parsed = Email::MIME->new($mail); my $from = encode('utf8', $parsed->header('From')); my $subject = encode('utf8', $parsed->header('Subject')); my $body = $parsed->body_str; say "$header"; say "$body\n"; } # END for loop # Close POP connection $pop->Close();

In reply to Mail::POP3Client basic program by gpinzone

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.