0

0

smtp邮件发送一例

php中文网

php中文网

发布时间:2016-06-01 14:31:36

|

1234人浏览过

|

来源于php中文网

原创

test_smtp.php


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("   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")   return(0);
  $this->pending_sender=0;
 }
 for(;$this->pending_recipients;$this->pending_recipients--)
 {
  if($this->VerifyResultLines(array("250","251"))   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: "))
  return(0);
 if(!IsSet($this->esmtp_extensions["PIPELINING"])
 && $this->VerifyResultLines("250")   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:"))
  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"))   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")   return(0);
 $this->state="SendingData";
 return(1);
}

Function 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;
   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")   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")   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")   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);
}

};

?>


热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
Golang 生态工具与框架:扩展开发能力
Golang 生态工具与框架:扩展开发能力

《Golang 生态工具与框架》系统梳理 Go 语言在实际工程中的主流工具链与框架选型思路,涵盖 Web 框架、RPC 通信、依赖管理、测试工具、代码生成与项目结构设计等内容。通过真实项目场景解析不同工具的适用边界与组合方式,帮助开发者构建高效、可维护的 Go 工程体系,并提升团队协作与交付效率。

1

2026.02.24

Golang 性能优化专题:提升应用效率
Golang 性能优化专题:提升应用效率

《Golang 性能优化专题》聚焦 Go 应用在高并发与大规模服务中的性能问题,从 profiling、内存分配、Goroutine 调度、GC 机制到 I/O 与锁竞争逐层分析。结合真实案例讲解定位瓶颈的方法与优化策略,帮助开发者建立系统化性能调优思维,在保证代码可维护性的同时显著提升服务吞吐与稳定性。

2

2026.02.24

Golang 面试题精选:高频问题与解答
Golang 面试题精选:高频问题与解答

Golang 面试题精选》系统整理企业常见 Go 技术面试问题,覆盖语言基础、并发模型、内存与调度机制、网络编程、工程实践与性能优化等核心知识点。每道题不仅给出答案,还拆解背后的设计原理与考察思路,帮助读者建立完整知识结构,在面试与实际开发中都能更从容应对复杂问题。

1

2026.02.24

Golang 运行与部署实战:从本地到云端
Golang 运行与部署实战:从本地到云端

《Golang 运行与部署实战》围绕 Go 应用从开发完成到稳定上线的完整流程展开,系统讲解编译构建、环境配置、日志与配置管理、容器化部署以及常见运维问题处理。结合真实项目场景,拆解自动化构建与持续部署思路,帮助开发者建立可靠的发布流程,提升服务稳定性与可维护性。

3

2026.02.24

Golang 疑难杂症解决指南:常见问题排查与优化
Golang 疑难杂症解决指南:常见问题排查与优化

《Golang 疑难杂症解决指南》聚焦开发过程中常见却棘手的问题,从并发模型、内存管理、性能瓶颈到工程化实践逐步拆解。通过真实案例与调试思路,帮助开发者定位问题根因,建立系统化排查方法。不只给出答案,更强调分析路径与工具使用,让你在复杂 Go 项目中具备持续解决问题的能力。

1

2026.02.24

Golang 入门学习路线:从零基础到上手开发
Golang 入门学习路线:从零基础到上手开发

Golang 入门路线涵盖从零到上手的核心路径:首先打牢基础语法与切片等底层机制;随后攻克 Go 的灵魂——接口设计与 Goroutine 并发模型;接着通过 Gin 框架与 GORM 深入 Web 开发实战;最后在微服务与云原生工具开发中进阶,旨在培养具备高性能并发处理能力的后端工程师。

0

2026.02.24

中国研究生招生信息网官方网站入口 研招网网页版在线入口
中国研究生招生信息网官方网站入口 研招网网页版在线入口

中国研究生招生信息网入口(https://yz.chsi.com.cn) 此网站是研究生报名入口的唯一官方网站

95

2026.02.24

苹果官网入口与在线访问指南_中国站点快速直达与iPhone查看方法
苹果官网入口与在线访问指南_中国站点快速直达与iPhone查看方法

本专题汇总苹果官网最新可用入口及中国站点访问方式,涵盖官网直达链接、iPhone官方页面查看方法与常见访问说明,帮助用户快速进入苹果官方网站,便捷了解产品信息与官方服务。

14

2026.02.24

Asianfanfics官网入口与访问指南_AFF官方平台最新登录地址
Asianfanfics官网入口与访问指南_AFF官方平台最新登录地址

本专题系统整理Asianfanfics(AFF)官方网站最新可用入口,涵盖官方平台最新直达地址、官网登录方式及中文访问指引,帮助用户快速、安全地进入AFF平台浏览与使用相关内容。

15

2026.02.24

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Webpack4.x---十天技能课堂
Webpack4.x---十天技能课堂

共20课时 | 1.5万人学习

Bootstrap4.x---十天精品课堂
Bootstrap4.x---十天精品课堂

共22课时 | 1.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号