<?php
error_reporting(11);
/**
*
*/
class smtp_mail
{
private $host; //保存要链接的SMTP服务器
private $port=25; //要绑定的端口,默认为25
private $user; //要登录SMTP的服务器的用户和密码
private $pass;
private $debug=false; //标识是否开始调试模式,默认为关闭
private $sock; //保存与SMTP服务端链接的句柄
private $mail_format=0; //标志使用什么格式发送邮件 0,为普通文件,1为HTML邮件
/*
初始化:
$param $host 主机名
$param $port 端口
$param $user 登录账号
$param $pass 登录密码
$param $format 类型格式 0文本 1Html
$param $debug 是否开启调试
*/
public function __construct($host,$port,$user,$pass,$format=1,$debug=0)
{
$this->host=$host;
$this->port=$port;
$this->user=base64_encode($user);
$this->pass=base64_encode($pass);
$this->mail_format=$format;
$this->debug=$debug;
$this->sock=fsockopen($this->host,$this->port,&$errno,&$errstr,3);
if(!$this->sock)
{
exit("Error number: $errno,Error message: $errstr\n");
}
$response=fgets($this->sock);
if(strstr($response,"220")===false)
{
exit("server error: $response\n");
}
}
/*
调试类。打印错误信息
*/
private function show_debug($message)
{
if($this->debug)
{
echo "<p>Debug: $message\n";
}
}
/*
沿着通道写信息
*/
private function do_command($cmd,$return_code)
{
fwrite($this->sock,$cmd);
$response=fgets($this->sock);
// echo $response."+".$return_code."----------";
if(strstr($response,"$return_code")===false)
{
$this->show_debug($response);
return false;
}
return true;
}
/*
判断是否是邮件
*/
private function is_email($email)
{
$pattren="/^[^_][\w]*@[\w.]+[\w]*[^_]$/";
if (preg_match($pattren,$email,$matches)) {
return true;
}else
{
return false;
}
}
/*
发送邮件
*/
public function send_mail($from,$to,$subject,$body)
{
//检查收|发人邮箱是否正确
if(!$this->is_email($from)OR !$this->is_email($to))
{
$this->show_debug("Please enter vaild from/to email");
return false;
}
//检查
if (empty($subject) OR empty($body))
{
$this->show_debug("Please enter subject/content");
return false;
}
$detail="From: ".$from."\r\n";
$detail.="To: ".$to."\r\n";
$detail.="Subject: ".$subject."\r\n";
if ($this->mail_format==1)
{
$detail .="Content-Type: text/html; \r\n";
}else
{
$detail .="Content-Type: text/plain; \r\n";
}
$detail.="charset=utf-8\r\n\r\n";
$detail.=$body;
$this->do_command("HELO ".$this->host."\r\n",250);
$this->do_command("AUTH LOGIN\r\n",334);
$this->do_command($this->user."\r\n",334);
$this->do_command($this->pass."\r\n",235);
$this->do_command("MAIL FROM: ".$from."\r\n",250);
$this->do_command("RCPT TO: ".$to."\r\n",250);
$this->do_command("DATA\r\n",354);
$this->do_command($detail."\r\n.\r\n",250);
$this->do_command("QUIT\r\n",221);
return true;
}
}
$host="smtp.qq.com";
$port=25;
$user="648462920@qq.com";
$pass="648462920jin";
$from="648462920@qq.com";
$to="295517247@qq.com";
$subject="HELO body ----php socket";
$content="大神有没有收到我的邮件";
$mail=new smtp_mail($host,$port,$user,$pass);
$mail->send_mail($from,$to,$subject,$content);
php-socket发送邮箱
最新推荐文章于 2018-04-11 16:42:46 发布