首页 > 开发 > PHP > 正文

smtp邮件发送一例

2024-05-04 23:01:49
字体:
来源:转载
供稿:网友
smtp邮件发送一例

test_smtp.php

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,nnit is just to let you know that your smtp class is working just fine.nnbye.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)=="rn")
{
$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,"$linern"))
{
$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)."rn";
$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("rn.")
|| $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]."rn";
if(($success=$this->senddata($header_data."rn")))
{
$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);
}

};

?>  


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表