here are a couple of functions you can use to pop to a mail server and retreive mail *Note requires PEAR (don't know who originally authored them so can't give credit...sorry. I do know that I've completely butchered them to make sure they worked for me
Code: Select all
// you need to set the constants...obviously
<?php
require_once('../includes/pop3.php');
require_once('Mail/mimeDecode.php'); //PEAR Mail_Mime Module
require_once('Mail/RFC822.php'); // PEAR Mail Module
error_reporting(E_ERROR);
set_error_handler("e;emailErrorHandler"e;);
tt_db_connect();
mysql_query("e;LOCK TABLE ttpop3last WRITE"e;);
$getlastsql = "e;SELECT lastretrieved FROM ttpop3last"e;;
$getlasthandle = mysql_query($getlastsql)
or trigger_error("e;Couldn't get last email time."e;, E_USER_ERROR);
$getlast = mysql_fetch_row($getlasthandle);
if (!$getlast || strtotime($getlastї0]) + POP3_WAIT <= time())
{
$updatelastsql = $getlast ? "e;UPDATE ttpop3last SET lastretrieved=NOW()"e; : "e;INSERT INTO ttpop3last (lastretrieved) VALUES (NOW())"e;;
mysql_query($updatelastsql);
mysql_query("e;UNLOCK TABLES"e;);
$handle = pop3_connect(POP3_SERVER, POP3_PORT, POP3_USER, POP3_PASS, POP3_APOP)
or trigger_error("e;Couldn't connect to pop3 server. Details: {$errstr}"e;, E_USER_ERROR);
$stats = pop3_stat($handle)
or trigger_error("e;Couldn't execute stat command. Details: {$errstr}"e;, E_USER_ERROR);
if ($statsї'count'] > 0)
{
if ($messages = pop3_uidl($handle))
$uidlist = TRUE; //we have a unique id for each message now
else
{
$uidlist = FALSE; //we do not have a unique id for each message yet
$messages = pop3_list($handle)
or trigger_error("e;Couldn't retrieve message list. Details: {$errstr}"e;, E_USER_ERROR);
}
foreach($messages as $msgid => $extrainfo)
{
if (!$uidlist)
{
//don't want to retrieve extra messages unless we need to
$message = pop3_retr($handle, $msgid);
$messageuid = md5(implode('', $message));
}
else
{
$messageuid = trim($extrainfo);
}
$isuniquehandle = mysql_query("e;SELECT COUNT(id) FROM mailtable WHERE mailid='{$messageuid}'"e;)
or trigger_error("e;Couldn't select unique IDs."e;, E_USER_ERROR);
$isunique = mysql_fetch_row($isuniquehandle);
if ($isuniqueї0] == 0)
{
if ($uidlist)
{
if (($message = pop3_retr($handle, $msgid)) === FALSE) //retrieve message now since it wasn't done above
{
//we don't want to break the loop, there may just be something wrong with this message
continue;
}
}
$params = array();
$paramsї'include_bodies'] = true;
$paramsї'decode_bodies'] = true;
$paramsї'decode_headers'] = true;
$paramsї'input'] = implode('', $message);
$structure = Mail_mimeDecode::decode($params);
$subject = isset($structure->headersї'subject']) ? $structure->headersї'subject'] : '';
$from = $structure->headersї'from'];
$from = Mail_RFC822::parseAddressList($from, POP3_DEFHOST, FALSE, TRUE);
$from = $fromї0]->mailbox . '@' . $fromї0]->host; //don't care about extra info
$datesent = strtotime($structure->headersї'date']);
$messageid = $structure->headersї'message-id'];
$emailinfo = getEmail($structure);
if (!isset($emailinfoї'text']))
{
$emailinfoї'text'] = isset($emailinfoї'html']) ? trim(unhtmlentities(strip_tags($emailinfoї'html']))) : "e;No email content"e;;
}
if (!isset($emailinfoї'html']))
{
$emailinfoї'html'] = isset($emailinfoї'text']) ? nl2br(htmlentities($emailinfoї'text'])) : "e;No email content"e;;
}
//lokesh....run your insert queries here
if (POP3_DELETE)
pop3_delete($handle, $msgid);
mysql_free_result($isuniquehandle);
} // end foreach message
} // end if statsї'count'] > 0
pop3_quit($handle);
} //end if last time is this time
mysql_query("e;UNLOCK TABLES"e;);
header("e;Expires: Mon, 26 Jul 1997 05:00:00 GMT"e;);
// always modified
header("e;Last-Modified: "e; . gmdate("e;D, d M Y H:i:s"e;) . "e; GMT"e;);
// HTTP/1.1
header("e;Cache-Control: no-store, no-cache, must-revalidate"e;);
header("e;Cache-Control: post-check=0, pre-check=0"e;, false);
// HTTP/1.0
header("e;Pragma: no-cache"e;);
// tell it that this is a gif
header("e;Content-Type: image/gif"e;);
readFile("e;../images/spacer.gif"e;);
function emailErrorHandler($errno, $errstr, $errfile, $errline)
{
global $handle;
pop3_quit($handle);
mysql_query("e;UNLOCK TABLES"e;);
header("e;Expires: Mon, 26 Jul 1997 05:00:00 GMT"e;);
// always modified
header("e;Last-Modified: "e; . gmdate("e;D, d M Y H:i:s"e;) . "e; GMT"e;);
// HTTP/1.1
header("e;Cache-Control: no-store, no-cache, must-revalidate"e;);
header("e;Cache-Control: post-check=0, pre-check=0"e;, false);
// HTTP/1.0
header("e;Pragma: no-cache"e;);
// tell it that this is a gif
header("e;Content-Type: image/gif"e;);
readFile("e;../images/spacer.gif"e;);
exit();
}
?>
Code: Select all
<?php
//pop3 stuff
$errno = '';
$errstr = '';
define('GETLENGTH', 512);
define('TIMEOUT', 200);
function pop3_connect($popserver, $popport, $username = '', $pass = '', $apop = FALSE, $echocmd = FALSE, $echortrn = FALSE)
{
global $errno, $errstr;
if (empty($username) || empty($pass))
{
$errno = -2;
$errstr = "e;Username and/or password not provided"e;;
return FALSE;
}
if ($echocmd) echo "e;CONNECTING TO {$popserver}:{$popport}\n"e;;
$handle = fsockopen($popserver, $popport, $errno, $errstr);
if (!$handle)
{
if ($echocmd) echo "e;CONNECTION FAILED\n"e;;
return $handle;
}
stream_set_blocking($handle, TRUE);
$response = fgets($handle, GETLENGTH);
$response = pop3_killcrlf($response);
if ($echortrn) echo "e;REPLY: {$response}\n"e;;
if (!pop3_isGoodReply($response))
{
if ($echocmd || $echortrn) echo "e;UNRECOGNIZED REPLY\n"e;;
pop3_quit($handle, $echocmd, $echortrn);
$errno = -2;
$errstr = "e;Server returned this unrecognized response: {$response}"e;;
return FALSE;
}
$confString = preg_replace("e;/^\+OK\s+<(.+)>/"e;, "e;\\1"e;, $response); //for apop
$command = "e;NOOP\r\n"e;;
fwrite($handle, $command, strlen($command));
if ($echocmd) echo "e;CMD: {$command}\n"e;;
$response = fgets($handle, GETLENGTH);
$response = pop3_killcrlf($response);
if ($echortrn) echo "e;REPLY: {$response}\n"e;;
if (pop3_isGoodReply($response))
{
if ($echocmd || $echortrn) echo "e;SERVER IS NOT RFC 1939 COMPLIANT\n"e;;
pop3_quit($handle, $echocmd, $echortrn);
$errno = -2;
$errstr = "e;POP3 Server did not comply with RFC 1939 for NOOP before authentication: {$response}"e;;
return FALSE;
}
if (!$apop)
{
$command = "e;USER {$username}\r\n"e;;
fwrite($handle, $command, strlen($command));
if ($echocmd) echo "e;CMD: {$command}\n"e;;
$response = fgets($handle, GETLENGTH);
$response = pop3_killcrlf($response);
if ($echortrn) echo "e;REPLY: {$response}\n"e;;
if (!pop3_isGoodReply($response))
{
if ($echocmd || $echortrn) echo "e;USERNAME DID NOT WORK\n"e;;
pop3_quit($handle, $echocmd, $echortrn);
$errno = -2;
$errstr = "e;Username did not return OK: {$response}\n"e;;
return FALSE;
}
$command = "e;PASS {$pass}\r\n"e;;
fwrite($handle, $command, strlen($command));
if ($echocmd) echo "e;CMD: {$command}\n"e;;
$response = fgets($handle, GETLENGTH);
$response = pop3_killcrlf($response);
if ($echortrn) echo "e;REPLY: {$response}\n"e;;
if (!pop3_isGoodReply($response))
{
if ($echocmd || $echortrn) echo "e;Password not authenticated\n"e;;
pop3_quit($handle, $echocmd, $echortrn);
$errno = -2;
$errstr = "e;Username or password not authenticated: {$response}\n"e;;
return FALSE;
}
}
else //apop
{
$confString .= $pass;
$confString = md5($confString);
$command = "e;APOP {$username} {$confString}\r\n"e;;
fwrite($handle, $command, strlen($command));
if ($echocmd) echo "e;CMD: {$command}\n"e;;
$response = fgets($handle, GETLENGTH);
$response = pop3_killcrlf($response);
if ($echortrn) echo "e;REPLY: {$response}\n"e;;
if (!pop3_isGoodReply($response))
{
if ($echocmd || $echortrn) echo "e;APOP LOGIN FAILED\n"e;;
pop3_quit($handle, $echocmd, $echortrn);
$errno = -2;
$errstr = "e;Apop login not authenticated: {$response}\n"e;;
return FALSE;
}
}
//now do noop again just to be sure we're authenticated and the server works
$command = "e;NOOP\r\n"e;;
fwrite($handle, $command, strlen($command));
if ($echocmd) echo "e;CMD: {$command}\n"e;;
$response = fgets($handle, GETLENGTH);
$response = pop3_killcrlf($response);
if ($echortrn) echo "e;REPLY: {$response}\n"e;;
if (!pop3_isGoodReply($response))
{
if ($echocmd || $echortrn) echo "e;SERVER IS NOT RFC 1939 COMPLIANT\n"e;;
pop3_quit($handle, $echocmd, $echortrn);
$errno = -2;
$errstr = "e;POP3 Server did not comply with RFC 1939 for NOOP after authentication: {$response}"e;;
return FALSE;
}
set_time_limit(TIMEOUT);
return $handle;
}
function pop3_stat($handle, $echocmd = FALSE, $echortrn = FALSE)
{
global $errno, $errstr;
$command = "e;STAT\r\n"e;;
fwrite($handle, $command, strlen($command));
if ($echocmd) echo "e;CMD: {$command}\n"e;;
$response = fgets($handle, GETLENGTH);
$response = pop3_killcrlf($response);
if ($echortrn) echo "e;REPLY: {$response}\n"e;;
if (!pop3_isGoodReply($response))
{
if ($echocmd || $echortrn) echo "e;Stat failed\n"e;;
$errno = -3;
$errstr = "e;Stat command failed: {$response}"e;;
return FALSE;
}
set_time_limit(TIMEOUT);
$temp = explode("e; "e;, $response);
return array("e;count"e; => intval($tempї1]), "e;length"e; => intval($tempї2]));
}
function pop3_list($handle, $msgNum = '', $echocmd = FALSE, $echortrn = FALSE)
{
global $errno, $errstr;
if (empty($msgNum))
{
$command = "e;LIST\r\n"e;;
fwrite($handle, $command, strlen($command));
if ($echocmd) echo "e;CMD: {$command}\n"e;;
$response = fgets($handle, GETLENGTH);
$response = pop3_killcrlf($response);
if ($echortrn) echo "e;REPLY: {$response}\n"e;;
if (!pop3_isGoodReply($response))
{
if ($echocmd || $echortrn) echo "e;List failed\n"e;;
$errno = -3;
$errstr = "e;List command failed: {$response}"e;;
return FALSE;
}
$messages = array();
while (preg_match("e;/^(\d+) (\d+)(.*)$/"e;, fgets($handle, GETLENGTH), $lineInfo))
{
if ($echortrn) echo "e;REPLY: {$lineInfoї0]}\n"e;;
$messagesїintval($lineInfoї1])] = intval($lineInfoї2]);
}
set_time_limit(TIMEOUT);
return $messages;
}
else
{
$command = "e;LIST {$msgNum}\r\n"e;;
fwrite($handle, $command, strlen($command));
if ($echocmd) echo "e;CMD: {$command}\n"e;;
$response = fgets($handle, GETLENGTH);
$response = pop3_killcrlf($response);
if ($echortrn) echo "e;REPLY: {$response}\n"e;;
if (!pop3_isGoodReply($response))
{
if ($echocmd || $echortrn) echo "e;List failed\n"e;;
$errno = -3;
$errstr = "e;List command failed: {$response}"e;;
return FALSE;
}
set_time_limit(TIMEOUT);
$temp = explode("e; "e;, $response);
return array("e;msgid"e; => $tempї1], "e;size"e; => $tempї2]);
}
}
function pop3_retr($handle, $msgid, $echocmd = FALSE, $echortrn = FALSE)
{
global $errno, $errstr;
$command = "e;RETR {$msgid}\r\n"e;;
fwrite($handle, $command, strlen($command));
if ($echocmd) echo "e;CMD: {$command}\n"e;;
$response = fgets($handle, GETLENGTH);
$response = pop3_killcrlf($response);
if ($echortrn) echo "e;REPLY: {$response}\n"e;;
if (!pop3_isGoodReply($response))
{
if ($echocmd || $echortrn) echo "e;Retr failed\n"e;;
$errno = -3;
$errstr = "e;Retr command failed: {$response}"e;;
return FALSE;
}
$msg = array();
$line = fgets($handle, GETLENGTH);
while (!preg_match("e;/^\.\r\n/"e;, $line))
{
$msgї] = preg_replace("e;/^\.\./"e;, "e;."e;, $line);
$line = fgets($handle, GETLENGTH);
if (empty($line)) break;
}
set_time_limit(TIMEOUT);
return $msg;
}
function pop3_delete($handle, $msgid, $echocmd = FALSE, $echortrn = FALSE)
{
global $errno, $errstr;
$command = "e;DELE {$msgid}\r\n"e;;
fwrite($handle, $command, strlen($command));
if ($echocmd) echo "e;CMD: {$command}\n"e;;
$response = fgets($handle, GETLENGTH);
$response = pop3_killcrlf($response);
if ($echortrn) echo "e;REPLY: {$response}\n"e;;
if (!pop3_isGoodReply($response))
{
if ($echocmd || $echortrn) echo "e;Dele failed\n"e;;
$errno = -3;
$errstr = "e;Dele command failed: {$response}"e;;
return FALSE;
}
set_time_limit(TIMEOUT);
return TRUE;
}
function pop3_reset($handle, $echocmd = FALSE, $echortrn = FALSE)
{
global $errno, $errstr;
$command = "e;RSET\r\n"e;;
fwrite($handle, $command, strlen($command));
if ($echocmd) echo "e;CMD: {$command}\n"e;;
$response = fgets($handle, GETLENGTH);
$response = pop3_killcrlf($response);
if ($echortrn) echo "e;REPLY: {$response}\n"e;;
if (!pop3_isGoodReply($response))
{
if ($echocmd || $echortrn) echo "e;Rset failed\n"e;;
$errno = -3;
$errstr = "e;Rset command failed: {$response}"e;;
return FALSE;
}
set_time_limit(TIMEOUT);
return TRUE;
}
function pop3_quit($handle, $echocmd = FALSE, $echortrn = FALSE)
{
global $errno, $errstr;
$command = "e;QUIT\r\n"e;;
@fwrite($handle, $command, strlen($command));
if ($echocmd) echo "e;CMD: {$command}\n"e;;
$response = @fgets($handle, GETLENGTH);
$response = pop3_killcrlf($response);
if ($echortrn) echo "e;REPLY: {$response}\n"e;;
@fclose($handle);
return TRUE;
}
//optional functions - may not be implemented by server
function pop3_top($handle, $msgid, $numlines = '0', $echocmd = FALSE, $echortrn = FALSE)
{
global $errno, $errstr;
$command = "e;TOP {$msgid} {$numlines}\r\n"e;;
fwrite($handle, $command, strlen($command));
if ($echocmd) echo "e;CMD: {$command}\n"e;;
$response = fgets($handle, GETLENGTH);
$response = pop3_killcrlf($response);
if ($echortrn) echo "e;REPLY: {$response}\n"e;;
if (!pop3_isGoodReply($response))
{
if ($echocmd || $echortrn) echo "e;Top failed\n"e;;
$errno = -3;
$errstr = "e;Top command failed: {$response}"e;;
return FALSE;
}
$msg = array();
$line = fgets($handle, GETLENGTH);
while (!preg_match("e;/^\.\r\n/"e;, $line))
{
$msgї] = preg_replace("e;/^\.\./"e;, "e;."e;, $line);
$line = fgets($handle, GETLENGTH);
if (empty($line)) break;
}
set_time_limit(TIMEOUT);
return $msg;
}
function pop3_uidl($handle, $msgid = '', $echocmd = FALSE, $echortrn = FALSE)
{
global $errno, $errstr;
if (empty($msgid))
{
$command = "e;UIDL\r\n"e;;
fwrite($handle, $command, strlen($command));
if ($echocmd) echo "e;CMD: {$command}\n"e;;
$response = fgets($handle, GETLENGTH);
$response = pop3_killcrlf($response);
if ($echortrn) echo "e;REPLY: {$response}\n"e;;
if (!pop3_isGoodReply($response))
{
if ($echocmd || $echortrn) echo "e;Uidl failed\n"e;;
$errno = -3;
$errstr = "e;Uidl command failed: {$response}"e;;
return FALSE;
}
$messages = array();
while (preg_match("e;/^(\d+) (.*)$/"e;, fgets($handle, GETLENGTH), $lineInfo))
{
if ($echortrn) echo "e;REPLY: {$lineInfoї0]}\n"e;;
$messagesїintval($lineInfoї1])] = $lineInfoї2];
}
set_time_limit(TIMEOUT);
return $messages;
}
else
{
$command = "e;UIDL {$msgid}\r\n"e;;
fwrite($handle, $command, strlen($command));
if ($echocmd) echo "e;CMD: {$command}\n"e;;
$response = fgets($handle, GETLENGTH);
$response = pop3_killcrlf($response);
if ($echortrn) echo "e;REPLY: {$response}\n"e;;
if (!pop3_isGoodReply($response))
{
if ($echocmd || $echortrn) echo "e;Uidl failed\n"e;;
$errno = -3;
$errstr = "e;Uidl command failed: {$response}"e;;
return FALSE;
}
set_time_limit(TIMEOUT);
$temp = explode("e; "e;, $response);
return array("e;msgid"e; => $tempї1], "e;uid"e; => $tempї2]);
}
}
//helper functions
function pop3_isGoodReply($response = '')
{
if (empty($response)) return false;
if (preg_match("e;/^\+OK/"e;, $response)) return true;
return false;
}
function pop3_killcrlf($what)
{
return preg_replace("e;/\r|\n/"e;, '', $what);
}