WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Class that allows the PHP developer to establish connections with a POP3 mail server amd be able to list, retrieve and delete mail messages from a given mail box.
Categories : Network, Email, PHP, PHP Classes Click here to Update Your Picture
Manuel Lemos
Date : Jan 17th 1999
Grade : 4 of 5 (graded 7 times)
Viewed : 18591
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Manuel Lemos
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

<?

/*
* Copyright (C) 1998, Manuel Lemos (mlemos@acm.org)
*
* Permission to use and modify this software and its
* documentation for any purpose other than its incorporation
* into a commercial product is hereby granted without fee,
* as long as the author is notified that this piece of software
* is being used in other applications.
* Permission to copy and distribute this software and its
* documentation only for non-commercial use is also granted
* without fee, provided, however, that the above copyright
* notice appear in all copies, that both that copyright notice
* and this permission notice appear in supporting documentation.
* The author makes no representations about the suitability
* of this software for any purpose. It is provided ``as is'',
* without express or implied warranty.
*/

/* put this class definition in a file named pop3.php */

class pop3_class
{
var $hostname="";
var $port=110;

/* Private variables - DO NOT ACCESS */

var $connection=0;
var $state="DISCONNECTED";
var $greeting="";
var $must_update=0;


/* Private methods - DO NOT CALL */

Function GetLine()
{
for($line="";;)
{
if(feof($this->connection))
return(0);
$line.=fgets($this->connection,100);
$length=strlen($line);
if($length>=2
&& substr($line,$length-2,2)=="\r\n")
return(substr($line,0,$length-2));
}
}

Function PutLine($line)
{
return(fputs($this->connection,"$line\r\n"));
}

Function OpenConnection()
{
if($this->hostname=="")
return("2 it was not specified a valid hostname");
switch(($this->connection=fsockopen($this->hostname,$this->port)))
{
case -3:
return("-3 socket could not be created");
case -4:
return("-4 dns lookup on hostname \"$hostname\" failed");
case -5:
return("-5 connection refused or timed out");
case -6:
return("-6 fdopen() call failed");
case -7:
return("-7 setvbuf() call failed");
default:
return("");
}
}

Function CloseConnection()
{
if($this->connection!=0)
{
fclose($this->connection);
$this->connection=0;
}
}

/* Public methods */

/* Open method - set the object variable $hostname to the POP3 server address. */

Function Open()
{
if($this->state!="DISCONNECTED")
return("1 a connection is already opened");
if(($error=$this->OpenConnection())!="")
return($error);
$this->greeting=$this->GetLine();
if(GetType($this->greeting)!="string"
|| strtok($this->greeting," ")!="+OK")
{
$this->CloseConnection();
return("3 POP3 server greeting was not found");
}
$this->greeting=strtok("\r\n");
$this->must_update=0;
$this->state="AUTHORIZATION";
return("");
}

/* Close method - this method must be called at least if there are any
messages to be deleted */

Function Close()
{
if($this->state=="DISCONNECTED")
return("no connection was opened");
if($this->must_update)
{
if($this->PutLine("QUIT")==0)
return("Could not send the QUIT command");
$response=$this->GetLine();
if(GetType($response)!="string")
return("Could not get quit command response");
if(strtok($response," ")!="+OK")
return("Could not quit the connection: ".strtok("\r\n"));
}
$this->CloseConnection();
$this->state="DISCONNECTED";
return("");
}

/* Login method - pass the user name and password of POP account. Set
$apop to 1 or 0 wether you want to login using APOP method or not. */

Function Login($user,$password,$apop)
{
if($this->state!="AUTHORIZATION")
return("connection is not in AUTHORIZATION state");
if($apop)
{
if($this->PutLine("APOP $user ".md5($this->greeting.$password))==0)
return("Could not send the APOP command");
$response=$this->GetLine();
if(GetType($response)!="string")
return("Could not get APOP login command response");
if(strtok($response," ")!="+OK")
return("APOP login failed: ".strtok("\r\n"));
}
else
{
if($this->PutLine("USER $user")==0)
return("Could not send the USER command");
$response=$this->GetLine();
if(GetType($response)!="string")
return("Could not get user login entry response");
if(strtok($response," ")!="+OK")
return("User error: ".strtok("\r\n"));
if($this->PutLine("PASS $password")==0)
return("Could not send the PASS command");
$response=$this->GetLine();
if(GetType($response)!="string")
return("Could not get login password entry response");
if(strtok($response," ")!="+OK")
return("Password error: ".strtok("\r\n"));
}
$this->state="TRANSACTION";
return("");
}

/* Statistics method - pass references to variables to hold the number of
messages in the mail box and the size that they take in bytes. */

Function Statistics($messages,$size)
{
if($this->state!="TRANSACTION")
return("connection is not in TRANSACTION state");
if($this->PutLine("STAT")==0)
return("Could not send the STAT command");
$response=$this->GetLine();
if(GetType($response)!="string")
return("Could not get the statistics command response");
if(strtok($response," ")!="+OK")
return("Could not get the statistics: ".strtok("\r\n"));
$messages=strtok(" ");
$size=strtok(" ");
return("");
}

/* ListMessages method - the $message argument indicates the number of a
message to be listed. If you specify an empty string it will list all
messages in the mail box. The $unique_id flag indicates if you want
to list the each message unique identifier, otherwise it will
return the size of each message listed. If you list all messages the
result will be returned in an array. */

Function ListMessages($message,$unique_id)
{
if($this->state!="TRANSACTION")
return("connection is not in TRANSACTION state");
if($unique_id)
$list_command="UIDL";
else
$list_command="LIST";
if($this->PutLine("$list_command $message")==0)
return("Could not send the $list_command command");
$response=$this->GetLine();
if(GetType($response)!="string")
return("Could not get message list command response");
if(strtok($response," ")!="+OK")
return("Could not get the message listing: ".strtok("\r\n"));
if($message=="")
{
for($messages=array();;)
{
$response=$this->GetLine();
if(GetType($response)!="string")
return("Could not get message list response");
if($response==".")
break;
$message=intval(strtok($response," "));
if($unique_id)
$messages[$message]=strtok(" ");
else
$messages[$message]=intval(strtok(" "));
}
return($messages);
}
else
{
$message=intval(strtok(" "));
return(intval(strtok(" ")));
}
}

/* RetrieveMessage method - the $message argument indicates the number of
a message to be listed. Pass a reference variables that will hold the
arrays of the $header and $body lines. The $lines argument tells how
many lines of the message are to be retrieved. Pass a negative number
if you want to retrieve the whole message. */

Function RetrieveMessage($message,$headers,$body,$lines)
{
if($this->state!="TRANSACTION")
return("connection is not in TRANSACTION state");
if($lines<0)
{
$command="RETR";
$arguments="$message";
}
else
{
$command="TOP";
$arguments="$message $lines";
}
if($this->PutLine("$command $arguments")==0)
return("Could not send the $command command");
$response=$this->GetLine();
if(GetType($response)!="string")
return("Could not get message retrieval command response");
if(strtok($response," ")!="+OK")
return("Could not retrieve the message: ".strtok("\r\n"));
for($headers=$body=array(),$line=0;;$line++)
{
$response=$this->GetLine();
if(GetType($response)!="string")
return("Could not retrieve the message");
switch($response)
{
case ".":
return("");
case "":
break 2;
default:
if(substr($response,0,1)==".")
$response=substr($response,1,strlen($response)-1);
break;
}
$headers[$line]=$response;
}
for($line=0;;$line++)
{
$response=$this->GetLine();
if(GetType($response)!="string")
return("Could not retrieve the message");
switch($response)
{
case ".":
return("");
default:
if(substr($response,0,1)==".")
$response=substr($response,1,strlen($response)-1);
break;
}
$body[$line]=$response;
}
return("");
}

/* DeleteMessage method - the $message argument indicates the number of
a message to be marked as deleted. Messages will only be effectively
deleted upon a successful call to the Close method. */

Function DeleteMessage($message)
{
if($this->state!="TRANSACTION")
return("connection is not in TRANSACTION state");
if($this->PutLine("DELE $message")==0)
return("Could not send the DELE command");
$response=$this->GetLine();
if(GetType($response)!="string")
return("Could not get message delete command response");
if(strtok($response," ")!="+OK")
return("Could not delete the message: ".strtok("\r\n"));
$this->must_update=1;
return("");
}

/* ResetDeletedMessages method - Reset the list of marked to be deleted
messages. No messages will be marked to be deleted upon a successful
call to this method. */

Function ResetDeletedMessages()
{
if($this->state!="TRANSACTION")
return("connection is not in TRANSACTION state");
if($this->PutLine("RSET")==0)
return("Could not send the RSET command");
$response=$this->GetLine();
if(GetType($response)!="string")
return("Could not get reset deleted messages command response");
if(strtok($response," ")!="+OK")
return("Could not reset deleted messages: ".strtok("\r\n"));
$this->must_update=0;
return("");
}

/* IssueNOOP method - Just pings the server to prevent it auto-close the
connection after an idle timeout (tipically 10 minutes). Not very
useful for most likely uses of this class. It's just here for
protocol support completeness. */

Function IssueNOOP()
{
if($this->state!="TRANSACTION")
return("connection is not in TRANSACTION state");
if($this->PutLine("NOOP")==0)
return("Could not send the NOOP command");
$response=$this->GetLine();
if(GetType($response)!="string")
return("Could not NOOP command response");
if(strtok($response," ")!="+OK")
return("Could not issue the NOOP command: ".strtok("\r\n"));
return("");
}
};

/* ---- pop3.php3 class file ends here. ---- */

?>

---- example file ----

<HTML>
<HEAD>
<TITLE>POP3 PHP class test</TITLE>
</HEAD>
<BODY>
<?
include("pop3.php3");

$user="mlemos";
$password="password";
$apop=0;
$pop3_connection=new pop3_class;
$pop3_connection->hostname="localhost";
if(($error=$pop3_connection->Open())=="")
{
echo "<PRE>Connected to the POP3 server $pop3_connection->hostname</PRE>\n";
if(($error=$pop3_connection->Login($user,$password,$apop))=="")
{
echo "<PRE>User $user logged in.</PRE>\n";
if(($error=$pop3_connection->Statistics(&$messages,&$size))=="")
{
echo "<PRE>There are $messages messages in the mail box with a total of $size bytes.</PRE>\n";
$result=$pop3_connection->ListMessages("",0);
if(GetType($result)=="array")
{
for(Reset($result),$message=0;$message<count($result);Next($result),$message++)
echo "<PRE>Message ,Key($result), - , ".$result[Key($result)]." , bytes.</PRE>\n";
$result=$pop3_connection->ListMessages("",1);
if(GetType($result)=="array")
{
for(Reset($result),$message=0;$message<count($result);Next($result),$message++)
echo "<PRE>Message ,Key($result), Unique ID - \"".$result[Key($result)]."\"</PRE>\n";
if($messages>0)
{
if(($error=$pop3_connection->RetrieveMessage(1,&$headers,&$body,2))=="")
{
echo "<PRE>Message 1:\n---Message headers starts below---</PRE>\n";
for($line=0;$line<count($headers);$line++)
echo "<PRE>",HtmlSpecialChars($headers[$line]),"</PRE>\n";
echo "<PRE>---Message headers ends above---\n---Message body starts below---</PRE>\n";
for($line=0;$line<count($body);$line++)
echo "<PRE>",HtmlSpecialChars($body[$line]),"</PRE>\n";
echo "<PRE>---Message body ends above---</PRE>\n";
if(($error=$pop3_connection->DeleteMessage(1))=="")
{
echo "<PRE>Marked message 1 for deletion.</PRE>\n";
if(($error=$pop3_connection->ResetDeletedMessages())=="")
{
echo "<PRE>Resetted the list of messages to be deleted.</PRE>\n";
}
}
}
}
if($error==""
&& ($error=$pop3_connection->Close())=="")
echo "<PRE>Disconnected from the POP3 server $pop3_connection->hostname</PRE>\n";

}
else
$error=$result;
}
else
$error=$result;
}
}
}
if($error!="")
echo "<H2>Error: ",HtmlSpecialChars($error),"</H2>";
?>

</BODY>
</HTML>



EMail class that supports MIME quoted printable encoding of message bodies and Q encoding of message headers.
Categories : Network, Email, PHP, PHP Classes
Class for sending mail with MIME attachments in multipart format using external sendmail, mimencode and zip
Categories : Email, Network, PHP, PHP Classes
Client classes for Dictionary servers UPDATED: 2000-06-06
Categories : Network, Search, Complete Programs, PHP Classes, PHP
POP3 Class
Categories : PHP Classes, PHP, Email
cPanel Email Accounts Creator
Categories : PHP, PHP Classes, Email, Form Processing, Web Services
A class for sending email; it has support for To:, Cc:, Bcc: and Reply-To: headers. It requires that you have sendmail installed.
Categories : Email, PHP Classes, PHP
PHP MIME Decoder. This class decodes Mime Encoded email message. Attachments are stored in a director. Works with Multipart/alternative, multipart/mixed etc. see http://p3mail.com for example.
Categories : PHP, PHP Classes, Email
Validator - A PHP class that can can be used for validating Email IDs and Dates
Categories : PHP, PHP Classes, Data Validation, Email, Date Time
Three Cool Classes and One Trick
Categories : PHP, PHP Classes, Graphics, Email
An email validation script that actually checks against the recipient's mail server.
Categories : Email, Complete Programs, PHP, Network, Debugging
base class to query the whois database
Categories : Network, PHP, PHP Classes
Sample usage of IPv6 and IPv4 with PHP
Categories : PHP, PHP Classes, Network
EAvalidator - This class can be used to validate an e-mail address by checking its domain.
Categories : PHP, PHP Classes, Email, Regexps
Password reminder
Categories : PHP, PHP Classes, Databases, MySQL, Mail
Form Elements Class
Categories : PHP, PHP Classes, Form Processing
 Prachya Pantuyakorn wrote :601
Hi, I`ve tested ur script. It`s very useful. Would u pls give me and idea to create mailbox by using php cript? 

Best regard,
Prachya