PHP实现163邮箱自动发送邮件


Posted in PHP onMarch 29, 2016

163邮箱大家都使用过吧,那么基于php如何实现163邮箱自动发送邮件功能呢,下面三水点靠木小编给大家分享具体实现代码:

想给大家展示下效果图:

PHP实现163邮箱自动发送邮件

demo.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta charset="utf-8"> 
<title>PHP利用smtp类发送邮件范例</title> 
</head> 
<body> 
<form action="sendmail.php" method="post"> 
<p>收件人:<input type="text" name="toemail" /></p> 
<p>标  题:<input type="text" name="title" /></p> 
<p>内  容:<textarea name="content" cols="50" rows="5"></textarea></p> 
<p><input type="submit" value="发送" /></p> 
</form> 
</body> 
</html>

sendmail.php

<meta charset="utf-8"> 
<?php 
/** 
* 注:本邮件类都是经过我测试成功了的,如果大家发送邮件的时候遇到了失败的问题,请从以下几点排查: 
* 1. 用户名和密码是否正确; 
* 2. 检查邮箱设置是否启用了smtp服务; 
* 3. 是否是php环境的问题导致; 
* 4. 将26行的$smtp->debug = false改为true,可以显示错误信息,然后可以复制报错信息到网上搜一下错误的原因 
*/ 
require_once "email.class.php"; 
//******************** 配置信息 ******************************** 
$smtpserver = "smtp.163.com";//SMTP服务器 
$smtpserverport =25;//SMTP服务器端口 
$smtpusermail = "onestopweb@163.com";//SMTP服务器的用户邮箱 
$smtpemailto = $_POST['toemail'];//发送给谁 
$smtpuser = "onestopweb";//SMTP服务器的用户帐号 
$smtppass = "123456";//SMTP服务器的用户密码 
$mailtitle = $_POST['title'];//邮件主题 
$mailcontent = "<h1>".$_POST['content']."</h1>";//邮件内容 
$mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件 
//************************ 配置信息 **************************** 
$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证. 
$smtp->debug = false;//是否显示发送的调试信息 
$state = $smtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype); 
echo "<div style='width:300px; margin:36px auto;'>"; 
if($state==""){ 
echo "对不起,邮件发送失败!请检查邮箱填写是否有误。"; 
echo "<a href='index.html'>点此返回</a>"; 
exit(); 
} 
echo "恭喜!邮件发送成功!!"; 
echo "<a href='index.html'>点此返回</a>"; 
echo "</div>"; 
?>

email.class.php

<?php 
class Smtp 
{ 
var $smtp_port; 
var $time_out; 
var $host_name; 
var $log_file; 
var $relay_host; 
var $debug; 
var $auth; 
var $user; 
var $pass; 
var $sock; 
function Smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass) 
{ 
$this->debug = FALSE; 
$this->smtp_port = $smtp_port; 
$this->relay_host = $relay_host; 
$this->time_out = 3600; 
$this->auth = $auth; 
$this->user = $user; 
$this->pass = $pass; 
$this->host_name = "localhost"; 
$this->log_file = ""; 
$this->sock = FALSE; 
} 
function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "") 
{ 
$mail_from = $this->get_address($this->strip_comment($from)); 
$body = ereg_replace("(^|(\r\n))(\.)", "\1.\3", $body); 
$header = "MIME-Version:1.0\r\n"; 
if($mailtype=="HTML"){ 
$header .= "Content-Type:text/html\r\n"; 
} 
$header .= "To: ".$to."\r\n"; 
if ($cc != "") { 
$header .= "Cc: ".$cc."\r\n"; 
} 
$header .= "From: $from<".$from.">\r\n"; 
$header .= "Subject: ".$subject."\r\n"; 
$header .= $additional_headers; 
$header .= "Date: ".date("r")."\r\n"; 
$header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n"; 
list($msec, $sec) = explode(" ", microtime()); 
$header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n"; 
$TO = explode(",", $this->strip_comment($to)); 
if ($cc != "") { 
$TO = array_merge($TO, explode(",", $this->strip_comment($cc))); 
} 
if ($bcc != "") { 
$TO = array_merge($TO, explode(",", $this->strip_comment($bcc))); 
} 
$sent = TRUE; 
foreach ($TO as $rcpt_to) { 
$rcpt_to = $this->get_address($rcpt_to); 
if (!$this->smtp_sockopen($rcpt_to)) { 
$this->log_write("Error: Cannot send email to ".$rcpt_to."\n"); 
$sent = FALSE; 
continue; 
} 
if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) { 
$this->log_write("E-mail has been sent to <".$rcpt_to.">\n"); 
} else { 
$this->log_write("Error: Cannot send email to <".$rcpt_to.">\n"); 
$sent = FALSE; 
} 
fclose($this->sock); 
$this->log_write("Disconnected from remote host\n"); 
} 
return $sent; 
} 
function smtp_send($helo, $from, $to, $header, $body = "") 
{ 
if (!$this->smtp_putcmd("HELO", $helo)) { 
return $this->smtp_error("sending HELO command"); 
} 
if($this->auth){ 
if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) { 
return $this->smtp_error("sending HELO command"); 
} 
if (!$this->smtp_putcmd("", base64_encode($this->pass))) { 
return $this->smtp_error("sending HELO command"); 
} 
} 
if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) { 
return $this->smtp_error("sending MAIL FROM command"); 
} 
if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) { 
return $this->smtp_error("sending RCPT TO command"); 
} 
if (!$this->smtp_putcmd("DATA")) { 
return $this->smtp_error("sending DATA command"); 
} 
if (!$this->smtp_message($header, $body)) { 
return $this->smtp_error("sending message"); 
} 
if (!$this->smtp_eom()) { 
return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]"); 
} 
if (!$this->smtp_putcmd("QUIT")) { 
return $this->smtp_error("sending QUIT command"); 
} 
return TRUE; 
} 
function smtp_sockopen($address) 
{ 
if ($this->relay_host == "") { 
return $this->smtp_sockopen_mx($address); 
} else { 
return $this->smtp_sockopen_relay(); 
} 
} 
function smtp_sockopen_relay() 
{ 
$this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n"); 
$this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out); 
if (!($this->sock && $this->smtp_ok())) { 
$this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n"); 
$this->log_write("Error: ".$errstr." (".$errno.")\n"); 
return FALSE; 
} 
$this->log_write("Connected to relay host ".$this->relay_host."\n"); 
return TRUE; 
} 
function smtp_sockopen_mx($address) 
{ 
$domain = ereg_replace("^.+@([^@]+)$", "\1", $address); 
if (!@getmxrr($domain, $MXHOSTS)) { 
$this->log_write("Error: Cannot resolve MX \"".$domain."\"\n"); 
return FALSE; 
} 
foreach ($MXHOSTS as $host) { 
$this->log_write("Trying to ".$host.":".$this->smtp_port."\n"); 
$this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out); 
if (!($this->sock && $this->smtp_ok())) { 
$this->log_write("Warning: Cannot connect to mx host ".$host."\n"); 
$this->log_write("Error: ".$errstr." (".$errno.")\n"); 
continue; 
} 
$this->log_write("Connected to mx host ".$host."\n"); 
return TRUE; 
} 
$this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n"); 
return FALSE; 
} 
function smtp_message($header, $body) 
{ 
fputs($this->sock, $header."\r\n".$body); 
$this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> ")); 
return TRUE; 
} 
function smtp_eom() 
{ 
fputs($this->sock, "\r\n.\r\n"); 
$this->smtp_debug(". [EOM]\n"); 
return $this->smtp_ok(); 
} 
function smtp_ok() 
{ 
$response = str_replace("\r\n", "", fgets($this->sock, 512)); 
$this->smtp_debug($response."\n"); 
if (!ereg("^[23]", $response)) { 
fputs($this->sock, "QUIT\r\n"); 
fgets($this->sock, 512); 
$this->log_write("Error: Remote host returned \"".$response."\"\n"); 
return FALSE; 
} 
return TRUE; 
} 
function smtp_putcmd($cmd, $arg = "") 
{ 
if ($arg != "") { 
if($cmd=="") $cmd = $arg; 
else $cmd = $cmd." ".$arg; 
} 
fputs($this->sock, $cmd."\r\n"); 
$this->smtp_debug("> ".$cmd."\n"); 
return $this->smtp_ok(); 
} 
function smtp_error($string) 
{ 
$this->log_write("Error: Error occurred while ".$string.".\n"); 
return FALSE; 
} 
function log_write($message) 
{ 
$this->smtp_debug($message); 
if ($this->log_file == "") { 
return TRUE; 
} 
$message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message; 
if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) { 
$this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n"); 
return FALSE;; 
} 
flock($fp, LOCK_EX); 
fputs($fp, $message); 
fclose($fp); 
return TRUE; 
} 
function strip_comment($address) 
{ 
$comment = "\([^()]*\)"; 
while (ereg($comment, $address)) { 
$address = ereg_replace($comment, "", $address); 
} 
return $address; 
} 
function get_address($address) 
{ 
$address = ereg_replace("([ \t\r\n])+", "", $address); 
$address = ereg_replace("^.*<(.+)>.*$", "\1", $address); 
return $address; 
} 
function smtp_debug($message) 
{ 
if ($this->debug) { 
echo $message; 
} 
} 
} 
?>

有关PHP实现163邮箱自动发送邮件功能小编就到此给大家介绍完了,希望对大家有所帮助!

PHP 相关文章推荐
php性能优化分析工具XDebug 大型网站调试工具
May 22 PHP
基于PHP CURL用法的深入分析
Jun 09 PHP
php结合ajax实现赞、顶、踩功能实例
May 12 PHP
ThinkPHP之R方法实例详解
Jun 20 PHP
php实现的mongodb操作类
May 28 PHP
php无限分类使用concat如何实现
Nov 05 PHP
UPUPW 更新 64 位 Apache 系列 PHP 7.0 正式版
Dec 08 PHP
PHP文件操作之获取目录下文件与计算相对路径的方法
Jan 08 PHP
php面向对象值单例模式
May 03 PHP
php 数组随机取值的简单实例
May 23 PHP
php求数组全排列,元素所有组合的方法总结
Mar 14 PHP
laravel ajax curd 搜索登录判断功能的实现
Apr 17 PHP
Laravel5.1数据库连接、创建数据库、创建model及创建控制器的方法
Mar 29 #PHP
PHP创建word文档的方法(平台无关)
Mar 29 #PHP
PHP中key和current,next的联合运用实例分析
Mar 29 #PHP
CodeIgniter基于Email类发邮件的方法
Mar 29 #PHP
PHP中抽象类、接口的区别与选择分析
Mar 29 #PHP
php实现图片缩略图的方法
Mar 29 #PHP
YII动态模型(动态表名)支持分析
Mar 29 #PHP
You might like
php采集速度探究总结(原创)
2008/04/18 PHP
PHP 观察者模式的实现代码
2013/05/10 PHP
图文介绍PHP添加Redis模块及连接
2015/07/28 PHP
php基础教程
2015/08/26 PHP
关于laravel框架中的常用目录路径函数
2019/10/23 PHP
如何在Laravel之外使用illuminate组件详解
2020/09/20 PHP
浅谈tudou土豆网首页图片延迟加载的效果
2010/06/23 Javascript
深入理解JavaScript系列(15) 函数(Functions)
2012/04/12 Javascript
jquery next nextAll nextUntil siblings的区别介绍
2013/10/05 Javascript
纯js分页代码(简洁实用)
2013/11/05 Javascript
Jquery中ajax方法data参数的用法小结
2014/02/12 Javascript
jQuery的$.proxy()应用示例介绍
2014/04/03 Javascript
window resize和scroll事件的基本优化思路
2014/04/29 Javascript
js中定义一个变量并判断其是否为空的方法
2014/05/13 Javascript
jQuery实现瀑布流布局
2014/12/12 Javascript
angularJS与bootstrap结合实现动态加载弹出提示内容
2015/10/16 Javascript
Javascript Event(事件)的传播与冒泡
2017/01/23 Javascript
原生js仿浏览器滚动条效果
2017/03/02 Javascript
bootstrap suggest下拉框使用详解
2017/04/10 Javascript
Swiper 4.x 使用方法(移动端网站的内容触摸滑动)
2018/05/17 Javascript
什么时候不能在 Node.js 中使用 Lock Files
2019/06/24 Javascript
Vue数据双向绑定底层实现原理
2019/11/22 Javascript
JavaScript 自定义html元素鼠标右键菜单功能
2019/12/02 Javascript
JS实现进度条动态加载特效
2020/03/25 Javascript
Ubuntu下创建虚拟独立的Python环境全过程
2017/02/10 Python
彻底理解Python list切片原理
2017/10/27 Python
python中复数的共轭复数知识点总结
2020/12/06 Python
CSS3 分类菜单效果
2019/05/27 HTML / CSS
Expedia爱尔兰:酒店、机票、租车及廉价假期
2017/01/02 全球购物
土木工程建筑专业毕业生求职信
2013/10/21 职场文书
理工学院学生自我鉴定
2014/02/23 职场文书
大学生考试作弊检讨书
2014/09/21 职场文书
公司门卫岗位职责
2015/04/13 职场文书
商场圣诞节活动总结
2015/05/06 职场文书
特种设备安全管理制度
2015/08/06 职场文书
创业计划书之干洗店
2019/09/10 职场文书