分享
 
 
 

smtp发邮件

王朝other·作者佚名  2006-12-16
窄屏简体版  字體: |||超大  

require("smtp.php");

$smtp=new smtp_class;

$smtp->host_name="mail.xiaocui.com";

$smtp->localhost="localhost";

$from="webmaster@xiaocui.com";

$to="root@xiaocui.com";

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);

}

}

}

||||||

Function MailFrom($sender)

{

if(strcmp($this->state,"Connected"))

{

$this->error="connection is not in the initial state";

return(0);

}

$this->error="";

if(!$this->PutLine("MAIL FROM: <".$sender.">"))

return(0);

if(!IsSet($this->esmtp_extensions["PIPELINING"])

&& $this->VerifyResultLines("250")<=0)

return(0);

$this->state="SenderSet";

if(IsSet($this->esmtp_extensions["PIPELINING"]))

$this->pending_sender=1;

$this->pending_recipients=0;

return(1);

}

Function SetRecipient($recipient)

{

switch($this->state)

{

case "SenderSet":

case "RecipientSet":

break;

default:

$this->error="connection is not in the recipient setting state";

return(0);

}

$this->error="";

if(!$this->PutLine("RCPT TO:<".$recipient.">"))

return(0);

if(IsSet($this->esmtp_extensions["PIPELINING"]))

{

$this->pending_recipients++;

if($this->pending_recipients>=$this->maximum_piped_recipients)

{

if(!$this->FlushRecipients())

return(0);

}

}

else

{

if($this->VerifyResultLines(array("250","251"))<=0)

return(0);

}

$this->state="RecipientSet";

return(1);

}

Function StartData()

{

if(strcmp($this->state,"RecipientSet"))

{

$this->error="connection is not in the start sending data state";

return(0);

}

$this->error="";

if(!$this->PutLine("DATA"))

return(0);

if($this->pending_recipients)

{

if(!$this->FlushRecipients())

return(0);

}

if($this->VerifyResultLines("354")<=0)

return(0);

$this->state="SendingData";

return(1);

}

Function PrepareData($data,&$output)

{

$length=strlen(&$data);

for($output="",$position=0;$position<$length;)

{

$next_position=$length;

for($current=$position;$current<$length;$current++)

{

switch($data[$current])

{

case "\n":

$next_position=$current+1;

break 2;

case "\r":

$next_position=$current+1;

if($data[$next_position]=="\n")

$next_position++;

break 2;

}

}

if($data[$position]==".")

$output.=".";

$output.=substr(&$data,$position,$current-$position)."\r\n";

$position=$next_position;

}

}

Function SendData($data)

{

if(strcmp($this->state,"SendingData"))

{

$this->error="connection is not in the sending data state";

return(0);

}

$this->error="";

return($this->PutData(&$data));

}

Function EndSendingData()

{

if(strcmp($this->state,"SendingData"))

{

$this->error="connection is not in the sending data state";

return(0);

}

$this->error="";

if(!$this->PutLine("\r\n.")

|| $this->VerifyResultLines("250")<=0)

return(0);

$this->state="Connected";

return(1);

}

Function ResetConnection()

{

switch($this->state)

{

case "Connected":

return(1);

case "SendingData":

$this->error="can not reset the connection while sending data";

return(0);

case "Disconnected":

$this->error="can not reset the connection before it is established";

return(0);

}

$this->error="";

if(!$this->PutLine("RSET")

|| $this->VerifyResultLines("250")<=0)

return(0);

$this->state="Connected";

return(1);

}

Function Disconnect($quit=1)

{

if(!strcmp($this->state,"Disconnected"))

{

$this->error="it was not previously established a SMTP connection";

return(0);

}

$this->error="";

if(!strcmp($this->state,"Connected")

&& $quit

&& (!$this->PutLine("QUIT")

|| $this->VerifyResultLines("221")<=0))

return(0);

fclose($this->connection);

$this->connection=0;

$this->state="Disconnected";

return(1);

}

Function SendMessage($sender,$recipients,$headers,$body)

{

if(($success=$this->Connect()))

{

if(($success=$this->MailFrom($sender)))

{

for($recipient=0;$recipient {

if(!($success=$this->SetRecipient($recipients[$recipient])))

break;

}

if($success

&& ($success=$this->StartData()))

{

for($header_data="",$header=0;$header $header_data.=$headers[$header]."\r\n";

if(($success=$this->SendData($header_data."\r\n")))

{

$this->PrepareData($body,&$body_data);

$success=$this->SendData($body_data);

}

if($success)

$success=$this->EndSendingData();

}

}

$disconnect_success=$this->Disconnect($success);

if($success)

$success=$disconnect_success;

}

return($success);

}

};

?>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有