打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
thinkphp 5.0整合phpmailer发送邮件,含完整模型验证实例
向客户发送邮件是网站的必备功能,应用场景非常广泛,例如:注册,找回密码等
目前QQ和网易都有免费企业邮箱可以使用,只需要使用域名即可注册
下面为大家分享在新版thinkphp 5.0版本中,如何使用phpmailer向邮箱发送邮件,本文附件中包含完整的demo案例,供有需要的开发者参考
首先创建一个发送模型(Send.php):
<?phpnamespace 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):
<?phpnamespace 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">&nbsp;</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.andetu.com/category/thinkphp
案例下载地址:http://www.andetu.com/code/1862
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
ThinkPHP 3.2 vendor()方法的深入研究及Phpqrcode的正确扩展
thinkphp中怎么使用phpmailer发送邮件
bootstrap-table表格插件之服务器端分页实例
python测试开发django -140.Bootstrap 缩略图
从 Bootstrap优站精选 学习得出总结
Spring Boot 发邮件和附件,超实用!
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服