telnet smtp.163.com 25
Trying 220.181.12.16... Connected to smtp.163.com. Escape character is '^]'. 220 163.com Anti-spam GT for Coremail System (163com[20141201])
HELO smtp.163.com
| 指令 | 作用 |
|---|---|
| HELO | 向对方邮件服务器发出的标识自己的身份的命令 |
| AUTH LOGIN | 即将进行身份认证 |
| MAIL FROM | 告诉对方本次邮件发送人是谁 |
| RCPT TO | 发送给谁 |
| DATA | 告诉对方本次邮件,接下来我们发送邮件具体内容了 |
| QUIT | 邮件内容输入完毕后,执行该指令退出 |
class Mailer
{
private $host;
private $port = 25;
private $user;
private $pass;
private $debug = false;
private $sock;
public function __construct($host,$port,$user,$pass,$debug = false)
{
$this->host = $host;
$this->port = $port;
$this->user = base64_encode($user); //用户名密码一定要使用base64编码才行
$this->pass = base64_encode($pass);
$this->debug = $debug;
//socket连接
$this->sock = fsockopen($this->host,$this->port);
if(!$this->sock){
exit('出错啦');
}
//读取smtp服务返回给我们的数据
$response = fgets($this->sock);
$this->debug($response);
//如果响应中有220返回码,说明我们连接成功了
if(strstr($response,'220') === false){
exit('出错啦');
}
}
//发送SMTP指令,不同指令的返回码可能不同
public function execCommand($cmd,$return_code){
fwrite($this->sock,$cmd);
$response = fgets($this->sock);
//输出调试信息
$this->debug('cmd:'.$cmd .';response:'.$response);
if(strstr($response,$return_code) === false){
return false;
}
return true;
}
public function sendMail($from,$to,$subject,$body){
//detail是邮件的内容,一定要严格按照下面的格式,这是协议规定的
$detail = 'From:'.$from."\r\n";
$detail .= 'To:'.$to."\r\n";
$detail .= 'Subject:'.$subject."\r\n";
$detail .= 'Content-Type: Text/html;'."\r\n";
$detail .= 'charset=gb2312'."\r\n\r\n";
$detail .= $body;
$this->execCommand("HELO ".$this->host."\r\n",250);
$this->execCommand("AUTH LOGIN\r\n",334);
$this->execCommand($this->user."\r\n",334);
$this->execCommand($this->pass."\r\n",235);
$this->execCommand("MAIL FROM:<".$from.">\r\n",250);
$this->execCommand("RCPT TO:<".$to.">\r\n",250);
$this->execCommand("DATA\r\n",354);
$this->execCommand($detail."\r\n.\r\n",250);
$this->execCommand("QUIT\r\n",221);
}
public function debug($message){
if($this->debug){
echo '<p>Debug:'.$message . PHP_EOL .'</p>';
}
}
public function __destruct()
{
fclose($this->sock);
}
}
$port = 25; $user = 'username'; //请替换成你自己的smtp用户名 $pass = 'pass'; //请替换成你自己的smtp密码 $host = 'smtp.163.com'; $from = 'xxxxx@163.com'; $to = 'xxxx@qq.com'; $body = 'hello world'; $subjet = '我是标题'; $mailer = new Mailer($host,$port,$user,$pass,true); $mailer->sendMail($from,$to,$subjet,$body);
Debug:220 163.com Anti-spam GT for Coremail System (163com[20141201]) Debug:cmd:HELO smtp.163.com ;response:250 OK Debug:cmd:AUTH LOGIN ;response:334 dXNlcm5hbWU6 Debug:cmd:aXR6aG91anVuYmxvZ0AxNjMuY29t ;response:334 UGFzc3dvcmQ6 Debug:cmd:QzBjSGRRNe32xiNGFYUE5oag== ;response:235 Authentication successful Debug:cmd:MAIL FROM: ;response:250 Mail OK Debug:cmd:RCPT TO:<380472723@qq.com> ;response:250 Mail OK Debug:cmd:DATA ;response:354 End data with . Debug:cmd:From:itzhoujunblog@163.com To:380472723@qq.com Subject:我是标题 Content-Type: Text/html; charset=gb2312 hello world . ;response:250 Mail OK queued as smtp11,D8CowACXHE5APdNYCo0hAQ--.19144S2 1490238785 Debug:cmd:QUIT ;response:221 Bye
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有