Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/articles/perl-tutorials/)
-   -   Accessing Mail via POP3 in Perl & PHP (http://www.go4expert.com/articles/accessing-mail-via-pop3-perl-php-t11603/)

pradeep 25Jun2008 00:13

Accessing Mail via POP3 in Perl & PHP
 

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: Perl

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.

Code: 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!


All times are GMT +5.5. The time now is 13:31.