smtp邮件发送一例
require("smtp.php");
$smtp=新的 smtp_class;
$smtp->host_name="mail.xiaocui.com";
$smtp->localhost="localhost";
$from="webmaster@xiaocui.com";
$to="root@xiaocui.com";
if($smtp->SendMessage(
$来自,
数组(
$至
),
数组(
“来自:$来自”,
“至:$至”,
“主题:测试 Manuel Lemos 的 SMTP 类”
),
“Hello $to,nn这只是为了让您知道您的 SMTP 类工作正常。nnBye.n”))
echo "消息已发送至 $to OK.n";
其他
echo "无法将消息发送到 $to.nError: ".$smtp->error."n"
?>
smtp.php
类 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;
/* 私有变量 - 请勿访问 */
var $state="已断开连接";
var $connection=0;
var $pending_recipients=0;
/* 私有方法 - 不要调用 */
函数 OutputDebug($message)
{
echo $message,"
n";
}
函数 GetLine()
{
for($line="";;)
{
if(feof($this->连接))
{
$this->error="从套接字读取时到达流末尾";
返回(0);
}
if(($data=fgets($this->连接,100))==false)
{
$this->error="无法从套接字读取行";
返回(0);
}
$line.=$data;
$length=strlen($line);
if($length>=2
&& substr($line,$length-2,2)=="rn")
{
$line=substr($line,0,$length-2);
if($this->调试)
$this->OutputDebug(" 返回($行);
}
}
}
函数 PutLine($line)
{
if($this->调试)
$this->OutputDebug("> $line");
if(!fputs($this->connection,"$linern"))
{
$this->error="无法将行写入套接字";
返回(0);
}
返回(1);
}
函数 PutData($data)
{
if(strlen($data))
{
if($this->调试)
$this->OutputDebug("> $data");
if(!fputs($this->连接,$data))
{
$this->error="无法将数据写入套接字";
返回(0);
}
}
返回(1);
}
函数VerifyResultLines($code,$responses="")
{
if(GetType($responses)!="array")
$responses=array();
取消设置($match_code);
while(($line=$this->GetLine($this->connection)))
{
if(IsSet($match_code))
{
if(strcmp(strtok($line," -"),$match_code))
{
$this->error=$line;
返回(0);
}
}
其他
{
$match_code=strtok($line," -");
if(GetType($code)=="数组")
{
for($codes=0;$codes
{
$this->error=$line;
返回(0);
}
}
其他
{
if(strcmp($match_code,$code))
{
$this->error=$line;
返回(0);
}
}
}
$responses[]=strtok("");
if(!strcmp($match_code,strtok($line," ")))
返回(1);
}
返回(-1);
}
函数 FlushRecipients()
{
if($this->pending_sender)
{
if($this->VerifyResultLines("250") 返回(0);
$this->pending_sender=0;
}
for(;$this->pending_recipients;$this->pending_recipients--)
{
if($this->VerifyResultLines(array("250","251")) 返回(0);
}
返回(1);
}
/* 公共方法 */
函数 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))))
{
开关($错误)
{
情况-3:
$this->error="无法创建-3套接字";
返回(0);
情况-4:
$this->error="-4 主机名 "".$host_name."" 上的 dns 查找失败";
返回(0);
情况-5:
$this->error="-5 连接被拒绝或超时";
返回(0);
情况-6:
$this->error="-6 fdopen() 调用失败";
返回(0);
案例-7:
$this->error="-7 setvbuf() 调用失败";
返回(0);
默认值:
$this->error=$error." 无法连接到主机 "".$this->host_name.""";
返回(0);
}
}
其他
{
if(!strcmp($localhost=$this->localhost,"")
&& !strcmp($localhost=getenv("SERVER_NAME"),"")
&& !strcmp($localhost=getenv("HOST"),""))
$localhost="localhost";
$成功=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("");
}
$成功=1;
}
}
if(!$成功
&& $this->PutLine("HELO $localhost")
&& $this->VerifyResultLines("250")>0)
$成功=1;
}
if($成功)
{
$this->state="已连接";
返回(1);
}
其他
{
fclose($this->连接);
$this->连接=0;
$this->state="已断开连接";
返回(0);
}
}
}
函数 MailFrom($sender)
{
if(strcmp($this->state,"已连接"))
{
$this->error="连接未处于初始状态";
返回(0);
}
$this->error="";
if(!$this->PutLine("邮件来自:"))
返回(0);
if(!IsSet($this->esmtp_extensions["PIPELINING"])
&& $this->VerifyResultLines("250") 返回(0);
$this->state="SenderSet";
if(IsSet($this->esmtp_extensions["PIPELINING"]))
$this->pending_sender=1;
$this->pending_recipients=0;
返回(1);
}
函数 SetRecipient($recipient)
{
开关($this->状态)
{
案例“SenderSet”:
案例“RecipientSet”:
休息;
默认值:
$this->error="连接未处于收件人设置状态";
返回(0);
}
$this->error="";
if(!$this->PutLine("RCPT TO:"))
返回(0);
if(IsSet($this->esmtp_extensions["PIPELINING"]))
{
$this->pending_recipients ;
if($this->pending_recipients>=$this->maximum_piped_recipients)
{
if(!$this->FlushRecipients())
返回(0);
}
}
其他
{
if($this->VerifyResultLines(array("250","251")) 返回(0);
}
$this->state="RecipientSet";
返回(1);
}
函数 StartData()
{
if(strcmp($this->state,"RecipientSet"))
{
$this->error="连接未处于开始发送数据状态";
返回(0);
}
$this->error="";
if(!$this->PutLine("DATA"))
返回(0);
if($this->pending_recipients)
{
if(!$this->FlushRecipients())
返回(0);
}
if($this->VerifyResultLines("354") 返回(0);
$this->state="SendingData";
返回(1);
}
函数PrepareData($data,&$output)
{
$length=strlen(&$data);
for($output="",$position=0;$position {
$next_position=$length;
for($current=$position;$current {
开关($数据[$当前])
{
案例“n”:
$next_position=$current 1;
休息 2;
案例“r”:
$next_position=$current 1;
if($data[$next_position]=="n")
$下一个位置;
休息 2;
}
}
if($data[$position]==".")
$output.=".";
$output.=substr(&$data,$position,$current-$position)."rn";
$position=$next_position;
}
}
函数 SendData($data)
{
if(strcmp($this->state,"SendingData"))
{
$this->error="连接未处于发送数据状态";
返回(0);
}
$this->error="";
return($this->PutData(&$data));
}
函数 EndSendingData()
{
if(strcmp($this->state,"SendingData"))
{
$this->error="连接未处于发送数据状态";
返回(0);
}
$this->error="";
if(!$this->PutLine("rn.")
|| $this->VerifyResultLines("250") 返回(0);
$this->state="已连接";
返回(1);
}
函数 ResetConnection()
{
开关($this->状态)
{
案例“已连接”:
返回(1);
案例“发送数据”:
$this->error="发送数据时无法重置连接";
返回(0);
案例“已断开连接”:
$this->error="在建立连接之前无法重置连接";
返回(0);
}
$this->error="";
if(!$this->PutLine("RSET")
|| $this->VerifyResultLines("250") 返回(0);
$this->state="已连接";
返回(1);
}
函数断开连接($quit=1)
{
if(!strcmp($this->state,"已断开连接"))
{
$this->error="之前未建立 SMTP 连接";
返回(0);
}
$this->error="";
if(!strcmp($this->state,"已连接")
&& $退出
&& (!$this->PutLine("QUIT")
|| $this->VerifyResultLines("221") 返回(0);
fclose($this->连接);
$this->连接=0;
$this->state="已断开连接";
返回(1);
}
函数 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
if(($success=$this->SendData($header_data."rn")))
{
$this->PrepareData($body,&$body_data);
$success=$this->SendData($body_data);
}
if($成功)
$success=$this->EndSendingData();
}
}
$disconnect_success=$this->断开连接($success);
if($成功)
$成功=$disconnect_success;
}
返回($成功);
}
};
?>

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

JWT是一种基于JSON的开放标准,用于在各方之间安全地传输信息,主要用于身份验证和信息交换。1.JWT由Header、Payload和Signature三部分组成。2.JWT的工作原理包括生成JWT、验证JWT和解析Payload三个步骤。3.在PHP中使用JWT进行身份验证时,可以生成和验证JWT,并在高级用法中包含用户角色和权限信息。4.常见错误包括签名验证失败、令牌过期和Payload过大,调试技巧包括使用调试工具和日志记录。5.性能优化和最佳实践包括使用合适的签名算法、合理设置有效期、

PHP8.1中的枚举功能通过定义命名常量增强了代码的清晰度和类型安全性。1)枚举可以是整数、字符串或对象,提高了代码可读性和类型安全性。2)枚举基于类,支持面向对象特性,如遍历和反射。3)枚举可用于比较和赋值,确保类型安全。4)枚举支持添加方法,实现复杂逻辑。5)严格类型检查和错误处理可避免常见错误。6)枚举减少魔法值,提升可维护性,但需注意性能优化。

SOLID原则在PHP开发中的应用包括:1.单一职责原则(SRP):每个类只负责一个功能。2.开闭原则(OCP):通过扩展而非修改实现变化。3.里氏替换原则(LSP):子类可替换基类而不影响程序正确性。4.接口隔离原则(ISP):使用细粒度接口避免依赖不使用的方法。5.依赖倒置原则(DIP):高低层次模块都依赖于抽象,通过依赖注入实现。

会话劫持可以通过以下步骤实现:1.获取会话ID,2.使用会话ID,3.保持会话活跃。在PHP中防范会话劫持的方法包括:1.使用session_regenerate_id()函数重新生成会话ID,2.通过数据库存储会话数据,3.确保所有会话数据通过HTTPS传输。

静态绑定(static::)在PHP中实现晚期静态绑定(LSB),允许在静态上下文中引用调用类而非定义类。1)解析过程在运行时进行,2)在继承关系中向上查找调用类,3)可能带来性能开销。

RESTAPI设计原则包括资源定义、URI设计、HTTP方法使用、状态码使用、版本控制和HATEOAS。1.资源应使用名词表示并保持层次结构。2.HTTP方法应符合其语义,如GET用于获取资源。3.状态码应正确使用,如404表示资源不存在。4.版本控制可通过URI或头部实现。5.HATEOAS通过响应中的链接引导客户端操作。

在PHP中,异常处理通过try,catch,finally,和throw关键字实现。1)try块包围可能抛出异常的代码;2)catch块处理异常;3)finally块确保代码始终执行;4)throw用于手动抛出异常。这些机制帮助提升代码的健壮性和可维护性。

匿名类在PHP中的主要作用是创建一次性使用的对象。1.匿名类允许在代码中直接定义没有名字的类,适用于临时需求。2.它们可以继承类或实现接口,增加灵活性。3.使用时需注意性能和代码可读性,避免重复定义相同的匿名类。
