PHP - Internet Mail Access Protocol - Using Sockets

From Code Trash
Jump to: navigation, search

Simple Program using IMAP Protocol

<?
set_time_limit(-1);
echo "<pre>";
function fetchuid($account, $password)
{
    $retval = 0;
    $fp = fsockopen('site or ip', 143);
    if ($fp > 0)
    {
        $buf = fgets($fp, 1024);
		echo $buf;
		echo "<br><br>";
		flush();
 
        fputs($fp, "A0001 LOGIN $account $password\r\n");
        $buf = fgets($fp, 1024);
		echo $buf;
		flush();
 
		$buf='';
        fputs($fp, "A0005 SELECT INBOX \r\n");
//        $buf = fgets($fp, 2048);
		$buf ='';
		$f = fopen("hai.txt","w");
		do
		{
			$df = fgets($fp);		
			echo $df;			
			flush();
		}while(strpos($df,"A0005 OK")===false);
 
		echo "<br><br>";
		flush();
 
        fputs($fp, "A023 LOGOUT\r\n");
        $buf = fgets($fp);
 
        fclose($fp);
    }
	echo "<br>";
    echo $buf;// substr($retval,6,30);
}
 
fetchuid('username@sitename.domain','password');
?>

Next step to the above simple program

<?
echo ini_get("default_socket_timeout");
set_time_limit(30);
 
echo "<pre>";
 
function sendres($fp,$command)
{
	static $gat=7;
        fputs($fp, "A000$gat $command\r\n");
//		echo "A000$gat $command";
		do
		{
			$df = fgets($fp);
			echo $df;
			flush();
		}while(strpos($df,"A000$gat OK")===false);
 
		echo "<br><br>";
		flush();
		$gat++;
}
 
function fetchuid($account, $password)
{
    $retval = 0;
    $fp = fsockopen('site or ip', 143);
 
    if ($fp > 0)
    {
        $buf = fgets($fp, 1024);
		echo $buf;
		echo "<br><br>";
		flush();
 
        fputs($fp, "A0001 LOGIN $account $password\r\n");
        $buf = fgets($fp, 1024);
		echo $buf;
		flush();
 
		stream_set_timeout($fp, 10);
 
 		sendres($fp,"SELECT INBOX");
 		sendres($fp,"FETCH 1 full");
		sendres($fp,"FETCH 1 body[text]");
 
        fputs($fp, "A0008 LOGOUT\r\n");
        $buf = fgets($fp);
 
        fclose($fp);
    }
	echo "<br>";
    echo $buf;// substr($retval,6,30);
}
 
fetchuid('user','pass');
?>