首頁 > 後端開發 > php教程 > smtp郵件發送一例_PHP教程

smtp郵件發送一例_PHP教程

WBOY
發布: 2016-07-21 16:01:14
原創
1043 人瀏覽過

test_smtp.php


require("smtp.php");

$smtp=new smtp_class;

$smtp->host_name=" smtp_class;

$smtp->host_name=" smtp_class;

$smmail. xiaocui.com";
$smtp->localhost="localhost";
$from="webmaster@xiaocui.com";
$to="root@xiaocui.com";
if ($smtp->SendMessage(
  $from,
  array(
   $to
  ),
  : $to",
   「主題:測試Manuel Lemos 的SMTP 類別」
  ),
  「您好$to,nn這只是為了讓您知道您的SMTP 類別運作良好。 ))
  echo "訊息已發送至$to OK.n";
else
  echo "無法將訊息傳送至$to.nError: ".$smtp-> 錯誤。 " 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;

/* 私人變數-recipients=100;

/* 私人變數-recipients=100;

/* 私人變數- 🎜>
var $state="Disconnected";
var $connection=0;
var $pending_recipients=0;

/* 私有方法- 請勿呼叫*/

函數OutputDebug($message)
{
  echo $message,"
n";
}

函數GetLine()
{
for( $line="";;)
  {
   if(feof($this->connection))
   {
    $this->error="到達串流末尾,同時從套接字讀取取";
    return(0);
   }
   if(($data=fgets($this->connection,100))==false) error="無法從套接字讀取行";
    return(0);
   }
   $line.=$data;
  if($length>=2
   && substr($line,$length-2,2)=="rn")
   {
    $line=substr($line,0 ,$length-2 );
    if($this->debug)
     $this->OutputDebug("    return($line);; }

函數PutLine($line)
{
  if($this- >調試)
   $this->OutputDebug("> $line");
   $this->OutputDebug("> $line");
   $this->OutputDebug("> $line");
   $! fputs($this->connection,"$linern"))
  {
$this->error="無法將行寫入套接字";
   return(0);
  }
  return(1);
}

函數PutData($data)
{
  if(strlen($data))debug)
    $this->OutputDebug("> $ data");
   if(!fputs($this->connection,$data))
  ="無法將資料寫入套接字";
    return(0);
   }
  }
  return(1);
}
Dis> code,$responses="")
{
  if(GetType($responses)!="array")
   $responses=array();
  取消設定($match_code);
 >
  while(($line=$this-> ;GetLine($this->連接)))
  {
   if(IsSet($match_code))    {
     $this->error=$line;
    else
   {
    $match_code=strtok ($line," -");
    if(GetType($code)=="array")     if($codes>=count($code))
  this->error=$line;
回(0);
     }
    }
    其他
    {
     if(strcmp($match_code,$code))
     return(0);
}
    }
   }
   $responses[]=strtok("");
   if(!strcmp($match_code,strtok(> ;
  }
  return(-1);
}

函數FlushRecipients()
{
  if($this->pending_s. if($this->VerifyResultLines("250")    return(0);
   $this->pending_sender=0;
 ;$this->pending_recipients--)
  {
   if($this->VerifyResultLines(array("250","251")) }
  return(1);
}

/* 公共方法*/

Function Connect()
{
  $this-> ="";
   $this->esmtp_host="";
   $this->esmtp_extensions=array();
  if(!($this ->connection=($open->timeout ?this-open ) ($this->host_name,$this->host_port,&$errno,&$error,$this->timeout) : fsockopen($this->host_port,&$errno,&$error,$this->timeout ) >host_name,$this->host_port))))
  {
   switch($error)
   {
    case -3:
套組接字";
     return(0);
    情況-4:
     $this->error="-4 主機名稱「」.$host_name.查找"" 上的 dns this->error="-4 主機名稱「」.$host_name.查找"" 上的 dns 0""" 失敗」;
     return(0);
    case -5:
     $this->error="-5連結被拒絕或逾時";
     $this->error="-6 fdopen() 呼叫失敗";
     return(0);
    case -7:
 ";
     return(0);
    預設值:
     $this->error=$error。" 無法連接主機「」.$this->host_name。 );
   }
  }
  else
  {
   if(!strcmp($localhost=$this->lohost,"$Aget gL(ccalcm "SERVER_NAME"),"")
   && !strcmp($localhost=getenv("HOST"),""))
     $localhost="localhost";VerifyResultLines("220")>0)
    {
     if($this->esmtp)
     if($this->esmtp)
      if($ this->PutLine("EHLO $localhost")
      && $this->VerifyResultLines("250",&$responses)>0)
{
== 0]," ");
       for($response=1;$response      ], " "));
        $this->esmtp_extensions[$extension]=strtok("");
       🎜>     }
     if(! $success
     && $this->PutLine("HELO $localhost")
     && $this->VerifyResultLines("250" )>0) > if($success)
   {
    $this->state="已連接";
    返回(1) ;
   }
   else
   {
    fclose($this->連接);
    $this->connection=0; return(0 );
   }
  }
}

函數MailFrom($sender)
{
  if(strcmp($this-this>"已連接到連接" ) ))
  {
   $this->error="連結未達成團結";
   return(0);
  }
  $this->error="" ; if( !$this->PutLine("mail來自:"))
   return(0);
  if(!IsSet($this- >esmtp_extensions["PIPELINING"] )
  && $this->VerifyResultLines("250")   return(0);
  $this->state="SenderSet" ;
>  $this->state="SenderSet" ;
 (Set($ >esmtp_extensions) ["PIPELINING"]))
   $this->pending_sender=1;
  $this->pending_recipients=0;
  return(1);
>函數SetRecipient( $recipient)
{
  switch($this->state)
  {
   case "SenderSet":    $this->error="連接不進行常規設定狀態";
    return(0);
  }
  $this->error=""PutLine("RCPT TO:"))
   return(0);
  if(IsSet($this->esmtp_extensions["PIPELINING"]))
  {
   $this->pending_recipients ;
   if($this->pending_recipients>=$this-> ;maximum_piped_recipients) 🎜 >     return (0);
     return (0);
   }
  }
  else
  {
   if ($this->VerifyResult {
   if ($this->VerifyResulto(1m >    return (0);
  }
  $this->state="RecipientSet";
  return(1);
}

函數 StartData()
{
  if(strcmp($this->state,"RecipientSet"))
>   $this->error="連線未開始傳送資料狀態";
   return(0);
  }
  $this->error="";
   (!$this- > PutLine("DATA"))
   return(0);
  if($this->pending_recipients)
  {
   if(!$this->Flush) );
  }
  if($this->VerifyResultLines("354")   return(0);
  $this-state="SendingData"; (1);
}

函數PrepareData($data,&$output)
{
  $length=strlen(&$data);
  for($output= " ",$position=0;$position  {
   $next_position=$length;
   for($current=$position; $current    switch($data[$current])
    {
      case "n":
    ":
      $next_position=$current 1;
      if($data[$next_position]=="n")
  
    }
   }
   if($data[$position]==".")
    $output.="位置,$current-$position)."rn";
   $position=$next_position;
  }
}

函數SendData($data)
}

函數SendData($data)
{state,"SendingData"))
  {
   $this->error="連線不傳送資料狀態";
   return(0);
  }
$this->error="";
  return($this->PutData(&$data));
}

函數EndSendingData()
{
  if(strcmp($this->state,"SendingData"))
  {
  傳送資料狀態";
   return(0);
  }
  $this->error="";
  if(!$this->PutLine("rn." )
  if(!$this->PutLine("rn." )
 | | $this->VerifyResultLines("250")   return(0);
  $this->state="已連接";
  return(1 );
}

函數ResetConnection()
{
  switch($this->state)
  {
   case "Connected ":
    $this->error="傳送資料時無法重設連線";
    return(0);
   case "Disconnected":
$this->error="在建立連線中建立連線之前無法重置連線";
    return(0);
  }
  $this->error="";
  if(! $this->PutLine("RSET")
  if(! $this->PutLine("RSET")
  if(! $this->PutLine("RSET")
|| $this->VerifyResultLines("250")   return(0);
  $this->state= "已連接";
  return(1);
}
}

函數斷開連接($quit=1)
{
  if(!strcmp($this->state,"已斷開連接"))
  {
   $ this->error="之前未建立SMTP 連線";
   return(0);
  }
  $this->error ="";
  if(!strcmp($this->error ="";
  if(!strcmp($this->state($this-state) ,"已連接")
  && $quit
  && (!$this->PutLine("QUIT")
  | | $this->VerifyResultLines("221")   return(0);
  fclose($this->connection);
  $this->connection=0;
  $this->state="Disconnected";

}

函數SendMessage($sender,$recipients,$headers,$body)
{
  if(($success=$this->Connect()))
{
   if(($success=$this->MailFrom($sender)))
   {
    for($recipient=0;$recipient     if(!($success=$this->SetRecipient($recipients[$recipient])) )
      $success= $this->StartData()))
    {
     for($header_data="", $header=0;$header   [$header]."rn";
     if(($success=$this->SendData ($header_data."rn")))
     {      $success=$this->SendData($body_data);
     }
    🎜>    }
   }
   $disconnect_success=$this->斷開連接($success) ;
   if($success)}

};

? >






http://www.bkjia.com/PHPjc/316851.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/316851.htmlTechArticletest_smtp.php ?需要(smtp.php); $smtp=新的smtp_class; $smtp-host_name=mail.xiaocui.com; $smtp-localhost=本機; $from=webmaster@xiaocui.com; $to=root@xiaocui.com ; if($smtp-Sen...
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板