Accessing Mail via POP3 in Perl & PHP

Discussion in 'Perl' started by pradeep, Jun 24, 2008.

  1. pradeep

    pradeep Team Leader

    Apr 4, 2005
    Likes Received:
    Trophy Points:
    Kolkata, India
    Home Page:


    POP3 stands for Post Office Protocol version 3, it's application layer Internet Protocol used by various desktop email clients, and web-based email services to fetch email from a remote server over a TCP/IP connection. Until sometime back POP3 was the de facto standard for email retriveal, but the recently popularity and additional features of IMAP has taken a big chunk of users from POP3. One of the notable reasons for popularity of IMAP over POP3 was when GMail starting providing free IMAP service, whereas Yahoo! Mail requires a paid subscription for POP3 access. Some popular POP3 email clients are Outlook Express, Microsoft Office Outlook, Mozilla Thunderbird, fetchmail, etc.

    POP3 had earlier versions of the protocol, informally called POP1 and POP2, now obsolete. In casual words POP almost always means POP3 in the context of e-mail protocols. All of us must have a at some point of time accessed mail using POP3, either in office or somewhere else. In this article we'll see how to programatically access and retrive email using POP3, we can use it for many purpose, just for checking new mails, automated backups, etc.

    Doing It With Perl

    For accessing emails using POP3 in perl we'll require the Perl module Net::POP3, you can get it from here. In the example below we'll connect to a POP3 server and retrive the mail headers and read who sent the mail, and also display total size of the mailbox.

     use Net::POP3;
     $pop3 = Net::POP3->new('mail.go4expert.coom'); ## create a new instance of Net::POP3, the POP3 server mostly runs on port 110
     if($pop3->login("", "myscretpassword")) ## try to login
         my $msg_numbers = $pop->list; # hashref of msg_number => size_of_the_mail
         my $total_mail_size = 0;
         print "Total messages on server: ",scalar keys %$msg_numbers,"\n\n";
         foreach my $msg_number (keys %$msg_numbers) 
             my $msg = $pop->top($msgnum,10); ## retrive first 10 lines of the mail
             $total_mail_size += $msg_numbers->{$msg_number};
             $msg = join '',@$msg;
             $msg =~ m!From\s*:\s*(.+)!;
             print "Mail from : $1 \n";
         print "\nTotal size of mails in the mailbox: $total_mail_size bytes\n";
         warn "Error Logging In";

    Doing It With PHP

    For doing the same in PHP, we'll require the PEAR package Net_POP3, get it from here. With the PEAR package it's a bit easier to get the headers, it already has an in-built method getParsedHeaders() which returns the parsed headers of the specified message. Let's go ahead and try it out.

     $pop3 = new Net_POP3();
    $strServerName "mail.go4expert.coom";
    $listing $pop3->getListing();
    "Total messages on server : ".count($listing)."\n\n";
    $listing as $msg)
    $headers $pop3->getParsedHeaders($msg['msg_id']);
    "Mail from : ".$headers['From']."\n";
    "\nTotal size of mails in the mailbox: ".$pop3->getSize()."\n";
    Hope this was helpful, enjoy coding!

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice