czcty(peter) ,有这样的提示?Fatal error: Call to undefined function imap_open() in /usr/local/apache/htdocs/php/netservice/aaa.php on line 2
php支持直接pop登录的,我曾经测试过,很简单
设置 php.ini 找到 imap 把前面的 ; 去了.
奇怪,我在PHP.ini里面没有找到imap相关项,我的服务器是LINUX的
POP3的Socket实例<? require("pop3.php"); $user="user"; $password="passwd"; $apop=0; $pop3_connection=new pop3_class; $pop3_connection->hostname="mail.xiaocui.com"; if(($error=$pop3_connection->Open())=="") { echo " Connected to the POP3 server "$pop3_connection->hostname". \n"; if(($error=$pop3_connection->Login($user,$password,$apop))=="") { echo " User "$user" logged in. \n"; if(($error=$pop3_connection->Statistics(&$messages,&$size))=="") { echo " There are $messages messages in the mail box with a total of $size bytes. \n"; $result=$pop3_connection->ListMessages("",0); if(GetType($result)=="array") { for(Reset($result),$message=0;$message echo " Message ",Key($result)," - ",$result[Key($result)]," bytes. \n"; if($messages>0) { if(($error=$pop3_connection->RetrieveMessage(1,&$headers,&$body,-1))=="") { echo " Message 1:\n---Message headers starts below--- \n"; for($line=0;$line echo " ",HtmlSpecialChars($headers[$line])," \n"; echo " ---Message headers ends above---\n---Message body starts below--- \n"; for($line=0;$line echo " ",HtmlSpecialChars($body[$line])," \n"; echo " ---Message body ends above--- \n";} } if($error==""&&($error=$pop3_connection->Close())=="") echo " Disconnected from the POP3 server "$pop3_connection->hostname". \n"; } else $error=$result; } } } if($error!="") echo " Error: ",HtmlSpecialChars($error)," "; ?> pop3.php class pop3_class { var $hostname=""; var $port=110;var $connection=0; var $state="DISCONNECTED"; var $greeting=""; var $must_update=0; var $debug=0;Function OutputDebug($message) { echo $message," \n"; }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") { $line=substr($line,0,$length-2); if($this->debug) $this->OutputDebug("< $line"); return($line); } } }Function PutLine($line) { if($this->debug) $this->OutputDebug("> $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; } }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(""); }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(""); }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(""); }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(" "))); } }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(""); }};?>
SMTP的Socket实例 <?
require("smtp.php"); $smtp=new smtp_class; $smtp->host_name="mail.xiaocui.com"; $smtp->localhost="localhost"; $from="[email protected]"; $to="[email protected]"; if($smtp->SendMessage( $from, array( $to ), array( "From: $from", "To: $to", "Subject: Testing Manuel Lemos" SMTP class" ), "Hello $to,\n\nIt is just to let you know that your SMTP class is working just fine.\n\nBye.\n")) echo "Message sent to $to OK.\n"; else echo "Cound not send the message to $to.\nError: ".$smtp->error."\n" ?>smtp.php
class smtp_class { var $host_name=""; var $host_port=25; var $localhost=""; var $timeout=0; var $error=""; var $debug=1; var $esmtp=1; var $esmtp_host=""; var $esmtp_extensions=array(); var $maximum_piped_recipients=100;/* private variables - DO NOT ACCESS */var $state="Disconnected"; var $connection=0; var $pending_recipients=0;/* Private methods - DO NOT CALL */Function OutputDebug($message) { echo $message," \n"; }Function GetLine() { for($line="";;) { if(feof($this->connection)) { $this->error="reached the end of stream while reading from socket"; return(0); } if(($data=fgets($this->connection,100))==false) { $this->error="it was not possible to read line from socket"; return(0); } $line.=$data; $length=strlen($line); if($length>=2 && substr($line,$length-2,2)=="\r\n") { $line=substr($line,0,$length-2); if($this->debug) $this->OutputDebug("< $line"); return($line); } } }Function PutLine($line) { if($this->debug) $this->OutputDebug("> $line"); if(!fputs($this->connection,"$line\r\n")) { $this->error="it was not possible to write line to socket"; return(0); } return(1); }Function PutData($data) { if(strlen($data)) { if($this->debug) $this->OutputDebug("> $data"); if(!fputs($this->connection,$data)) { $this->error="it was not possible to write data to socket"; return(0); } } return(1); }Function VerifyResultLines($code,$responses="") { if(GetType($responses)!="array") $responses=array(); Unset($match_code);while(($line=$this->GetLine($this->connection))) { if(IsSet($match_code)) { if(strcmp(strtok($line," -"),$match_code)) { $this->error=$line; return(0); } } else { $match_code=strtok($line," -"); if(GetType($code)=="array") { for($codes=0;$codes if($codes>=count($code)) { $this->error=$line; return(0); } } else { if(strcmp($match_code,$code)) { $this->error=$line; return(0); } } } $responses[]=strtok(""); if(!strcmp($match_code,strtok($line," "))) return(1); } return(-1); }Function FlushRecipients() { if($this->pending_sender) { if($this->VerifyResultLines("250")<=0) return(0); $this->pending_sender=0; } for(;$this->pending_recipients;$this->pending_recipients--) { if($this->VerifyResultLines(array("250","251"))<=0) return(0); } return(1); }/* Public methods */Function Connect() { $this->error=$error=""; $this->esmtp_host=""; $this->esmtp_extensions=array(); if(!($this->connection=($this->timeout ? fsockopen($this->host_name,$this->host_port,&$errno,&$error,$this->timeout) : fsockopen($this->host_name,$this->host_port)))) { switch($error) { case -3: $this->error="-3 socket could not be created"; return(0); case -4: $this->error="-4 dns lookup on hostname \"".$host_name."\" failed"; return(0); case -5: $this->error="-5 connection refused or timed out"; return(0); case -6: $this->error="-6 fdopen() call failed"; return(0); case -7: $this->error="-7 setvbuf() call failed"; return(0); default: $this->error=$error." could not connect to the host \"".$this->host_name."\""; return(0); } } else { if(!strcmp($localhost=$this->localhost,"") && !strcmp($localhost=getenv("SERVER_NAME"),"") && !strcmp($localhost=getenv("HOST"),"")) $localhost="localhost"; $success=0; if($this->VerifyResultLines("220")>0) { if($this->esmtp) { $responses=array(); if($this->PutLine("EHLO $localhost") && $this->VerifyResultLines("250",&$responses)>0) { $this->esmtp_host=strtok($responses[0]," "); for($response=1;$response { $extension=strtoupper(strtok($responses[$response]," ")); $this->esmtp_extensions[$extension]=strtok(""); } $success=1; } } if(!$success && $this->PutLine("HELO $localhost") && $this->VerifyResultLines("250")>0) $success=1; } if($success) { $this->state="Connected"; return(1); } else { fclose($this->connection); $this->connection=0; $this->state="Disconnected"; return(0); } } }
socket通信操作都是一回事啊
$mp=imap_open("{edu.pop.chinaren.com/pop3:110}INBOX","macsum","test");//username: macsum,pwd:test
$num=imap_num_msg($mp);
echo $num;
?>
require("pop3.php");
$user="user";
$password="passwd";
$apop=0;
$pop3_connection=new pop3_class;
$pop3_connection->hostname="mail.xiaocui.com";
if(($error=$pop3_connection->Open())=="")
{
echo "
Connected to the POP3 server "$pop3_connection->hostname".
\n";
if(($error=$pop3_connection->Login($user,$password,$apop))=="")
{
echo "
User "$user" logged in.
\n";
if(($error=$pop3_connection->Statistics(&$messages,&$size))=="")
{
echo "
There are $messages messages in the mail box with a total of $size bytes.
\n";
$result=$pop3_connection->ListMessages("",0);
if(GetType($result)=="array")
{
for(Reset($result),$message=0;$message echo "
Message ",Key($result)," - ",$result[Key($result)]," bytes.
\n";
if($messages>0)
{
if(($error=$pop3_connection->RetrieveMessage(1,&$headers,&$body,-1))=="")
{
echo "
Message 1:\n---Message headers starts below---
\n";
for($line=0;$line echo "
",HtmlSpecialChars($headers[$line]),"
\n";
echo "
---Message headers ends above---\n---Message body starts below---
\n";
for($line=0;$line echo "
",HtmlSpecialChars($body[$line]),"
\n";
echo "
---Message body ends above---
\n";}
}
if($error==""&&($error=$pop3_connection->Close())=="")
echo "
Disconnected from the POP3 server "$pop3_connection->hostname".
\n";
}
else
$error=$result;
}
}
}
if($error!="")
echo "
Error: ",HtmlSpecialChars($error),"
";
?>
pop3.php
class pop3_class
{
var $hostname="";
var $port=110;var $connection=0;
var $state="DISCONNECTED";
var $greeting="";
var $must_update=0;
var $debug=0;Function OutputDebug($message)
{
echo $message,"
\n";
}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")
{
$line=substr($line,0,$length-2);
if($this->debug)
$this->OutputDebug("< $line");
return($line);
}
}
}Function PutLine($line)
{
if($this->debug)
$this->OutputDebug("> $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;
}
}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("");
}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("");
}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("");
}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(" ")));
}
}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("");
}};?>
<?
require("smtp.php");
$smtp=new smtp_class;
$smtp->host_name="mail.xiaocui.com";
$smtp->localhost="localhost";
$from="[email protected]";
$to="[email protected]";
if($smtp->SendMessage(
$from,
array(
$to
),
array(
"From: $from",
"To: $to",
"Subject: Testing Manuel Lemos" SMTP class"
),
"Hello $to,\n\nIt is just to let you know that your SMTP class is working just fine.\n\nBye.\n"))
echo "Message sent to $to OK.\n";
else
echo "Cound not send the message to $to.\nError: ".$smtp->error."\n"
?>smtp.php
class smtp_class
{
var $host_name="";
var $host_port=25;
var $localhost="";
var $timeout=0;
var $error="";
var $debug=1;
var $esmtp=1;
var $esmtp_host="";
var $esmtp_extensions=array();
var $maximum_piped_recipients=100;/* private variables - DO NOT ACCESS */var $state="Disconnected";
var $connection=0;
var $pending_recipients=0;/* Private methods - DO NOT CALL */Function OutputDebug($message)
{
echo $message,"
\n";
}Function GetLine()
{
for($line="";;)
{
if(feof($this->connection))
{
$this->error="reached the end of stream while reading from socket";
return(0);
}
if(($data=fgets($this->connection,100))==false)
{
$this->error="it was not possible to read line from socket";
return(0);
}
$line.=$data;
$length=strlen($line);
if($length>=2
&& substr($line,$length-2,2)=="\r\n")
{
$line=substr($line,0,$length-2);
if($this->debug)
$this->OutputDebug("< $line");
return($line);
}
}
}Function PutLine($line)
{
if($this->debug)
$this->OutputDebug("> $line");
if(!fputs($this->connection,"$line\r\n"))
{
$this->error="it was not possible to write line to socket";
return(0);
}
return(1);
}Function PutData($data)
{
if(strlen($data))
{
if($this->debug)
$this->OutputDebug("> $data");
if(!fputs($this->connection,$data))
{
$this->error="it was not possible to write data to socket";
return(0);
}
}
return(1);
}Function VerifyResultLines($code,$responses="")
{
if(GetType($responses)!="array")
$responses=array();
Unset($match_code);while(($line=$this->GetLine($this->connection)))
{
if(IsSet($match_code))
{
if(strcmp(strtok($line," -"),$match_code))
{
$this->error=$line;
return(0);
}
}
else
{
$match_code=strtok($line," -");
if(GetType($code)=="array")
{
for($codes=0;$codes if($codes>=count($code))
{
$this->error=$line;
return(0);
}
}
else
{
if(strcmp($match_code,$code))
{
$this->error=$line;
return(0);
}
}
}
$responses[]=strtok("");
if(!strcmp($match_code,strtok($line," ")))
return(1);
}
return(-1);
}Function FlushRecipients()
{
if($this->pending_sender)
{
if($this->VerifyResultLines("250")<=0)
return(0);
$this->pending_sender=0;
}
for(;$this->pending_recipients;$this->pending_recipients--)
{
if($this->VerifyResultLines(array("250","251"))<=0)
return(0);
}
return(1);
}/* Public methods */Function Connect()
{
$this->error=$error="";
$this->esmtp_host="";
$this->esmtp_extensions=array();
if(!($this->connection=($this->timeout ? fsockopen($this->host_name,$this->host_port,&$errno,&$error,$this->timeout) : fsockopen($this->host_name,$this->host_port))))
{
switch($error)
{
case -3:
$this->error="-3 socket could not be created";
return(0);
case -4:
$this->error="-4 dns lookup on hostname \"".$host_name."\" failed";
return(0);
case -5:
$this->error="-5 connection refused or timed out";
return(0);
case -6:
$this->error="-6 fdopen() call failed";
return(0);
case -7:
$this->error="-7 setvbuf() call failed";
return(0);
default:
$this->error=$error." could not connect to the host \"".$this->host_name."\"";
return(0);
}
}
else
{
if(!strcmp($localhost=$this->localhost,"")
&& !strcmp($localhost=getenv("SERVER_NAME"),"")
&& !strcmp($localhost=getenv("HOST"),""))
$localhost="localhost";
$success=0;
if($this->VerifyResultLines("220")>0)
{
if($this->esmtp)
{
$responses=array();
if($this->PutLine("EHLO $localhost")
&& $this->VerifyResultLines("250",&$responses)>0)
{
$this->esmtp_host=strtok($responses[0]," ");
for($response=1;$response {
$extension=strtoupper(strtok($responses[$response]," "));
$this->esmtp_extensions[$extension]=strtok("");
}
$success=1;
}
}
if(!$success
&& $this->PutLine("HELO $localhost")
&& $this->VerifyResultLines("250")>0)
$success=1;
}
if($success)
{
$this->state="Connected";
return(1);
}
else
{
fclose($this->connection);
$this->connection=0;
$this->state="Disconnected";
return(0);
}
}
}