向客户发送邮件是网站的必备功能,应用场景非常广泛,例如:注册,找回密码等 目前QQ和网易都有免费企业邮箱可以使用,只需要使用域名即可注册 下面为大家分享在新版thinkphp 5.0版本中,如何使用phpmailer向邮箱发送邮件,本文附件中包含完整的demo案例,供有需要的开发者参考 首先创建一个发送模型(Send.php): <?php
namespace app\index\model;
use think\Validate;
class Send extends \think\Model
{
public static $email_config = [
'host' => 'smtp.exmail.qq.com',//邮箱host地址
'username' => '',//邮箱账号
'password' => '',//邮箱密码
'from' => '',//来自哪儿,一般为邮箱账号即可
'fromname' => '安德兔',//发件人名称
'altbody' => '安德兔注册验证码,如果您看见的是本条内容请与安德兔管理员联系!',//邮件默认内容,当收件人屏蔽了内容或某些意外情况时展现
];
public function email($data=[])
{
$validate = new Validate([
['email','require|email','邮箱输入错误|邮箱输入错误'],
['subject','require','请输入邮件标题'],
['message','require','请输入邮件内容'],
]);
if (!$validate->check($data)) {
return $validate->getError();
}
$config = self::$email_config;
vendor('phpmailer.phpmailer');
$phpmailer = new \phpmailer(); //实例化
$phpmailer->Host = $config['host']; //smtp服务器的名称(这里以QQ邮箱为例)
$phpmailer->SMTPAuth = TRUE; //启用smtp认证
$phpmailer->Username = $config['username']; //你的邮箱名
$phpmailer->Password = $config['password']; //邮箱密码
$phpmailer->From = $config['from']; //发件人地址(也就是你的邮箱地址)
$phpmailer->FromName = $config['fromname']; //发件人姓名
$phpmailer->CharSet = 'utf-8'; //设置邮件编码
$phpmailer->Subject = $data['subject']; //邮件主题
$phpmailer->Body = $data['message']; //邮件内容
$phpmailer->AltBody = $config['altbody']; //邮件正文不支持HTML的备用显示
$phpmailer->WordWrap = 50; //设置每行字符长度
$phpmailer->IsSMTP(true); // 启用SMTP
$phpmailer->IsHTML(true); // 是否HTML格式邮件
$phpmailer->AddAddress($data['email']);
$status = $phpmailer->Send();
return true;
}
}
?>
然后在控制器中调用模型来实现邮件发送(Index.php): <?php
namespace app\index\controller;
use app\index\model\Send;
error_reporting(0);
class Index extends \think\Controller
{
public function email()
{
if(request()->isPost()){
$Send = new Send;
$result = $Send->email([
'email' => input('post.email/s','','trim,strip_tags'),
'subject' => input('post.subject/s','','trim,strip_tags'),
'message' => input('post.message/s','','trim,strip_tags'),
]);
if($result !== true){
return $this->error($result);
}
return $this->success('邮件发送成功');
}
return $this->fetch();
}
}
最后再创建一个测试模板文件(email.html): <!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>邮件发送测试</title>
<base href="{:request()->domain()}" />
<link href="static/css/bootstrap.css" rel="stylesheet">
<link href="static/css/common.css" rel="stylesheet">
<link href="static/css/admin.css" rel="stylesheet">
<script src="static/js/jquery-1.12.0.min.js"></script>
<script src="static/js/bootstrap.min.js"></script>
<script src="static/js/jquery.qrcode.min.js"></script>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon"/>
</head>
<body>
<div class="container">
<div class="panel panel-default">
<div class="panel-heading">
<strong>邮件发送测试</strong>
</div>
<div class="panel-body">
<form class="form-horizontal email-form" method="post" action="{:url('index/index/email')}">
<div class="form-group">
<label class="col-sm-2 control-label">收件人邮箱</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="email" value="">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">邮件标题</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="subject" value="测试邮件">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">邮件内容</label>
<div class="col-sm-10">
<textarea class="form-control" row="6" name="message">测试邮件内容</textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">发送邮件</button>
</div>
</div>
</form>
</div>
<div class="panel-footer"> </div>
</div>
</div>
<script>
$(function(){
$('.email-form').submit(function(){
var $this = $(this);
if(!$this.hasClass('lock-form')){
$this.addClass('lock-form');//锁定表单
var formData = new FormData($this[0]);
$.ajax({
url:$this.attr("action"),
type:'POST',
data:formData,
dataType:'json',
cache: false,
contentType: false,
processData: false,
success:function(s){
$this.removeClass('lock-form');//解锁表单
$('.panel-footer').html(s.msg);
return false;
}
});
}
return false;
});
});
</script>
</body>
</html>
本功能涉及到的第三方类库存放于:vendor/文件夹下(phpmailer) THINKPHP系列教程地址:http://www./category/thinkphp 案例下载地址:http://www./code/1862
|