Accessing Mail via POP3 in Perl & PHP

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

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in

    Introduction



    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.

    Code:
     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("pradeep@go4expet.com", "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";
     }
     else
     {
         warn "Error Logging In";
     }
     
     $pop->quit;
     

    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.

    PHP:
     $pop3 = new Net_POP3();
     
    $strServerName "mail.go4expert.coom";
     
    $pop3->connect($strServerName);
     
     
    $pop3->login("pradeep@go4expet.com""myscretpassword");
     
    $listing $pop3->getListing();
     print 
    "Total messages on server : ".count($listing)."\n\n";
     
     foreach(
    $listing as $msg)
     {
         
    $headers $pop3->getParsedHeaders($msg['msg_id']);
         print 
    "Mail from : ".$headers['From']."\n";
     }
     
     print 
    "\nTotal size of mails in the mailbox: ".$pop3->getSize()."\n";
     
     
    $pop3->disconnect();
     
    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