向客戶發(fā)送郵件是網(wǎng)站的必備功能,應用場景非常廣泛,例如:注冊,找回密碼等
目前QQ和網(wǎng)易都有免費企業(yè)郵箱可以使用,只需要使用域名即可注冊
下面為大家分享在新版thinkphp 5.0版本中,如何使用phpmailer向郵箱發(fā)送郵件,本文附件中包含完整的demo案例,供有需要的開發(fā)者參考
首先創(chuàng)建一個發(fā)送模型(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' => '安德兔',//發(fā)件人名稱 'altbody' => '安德兔注冊驗證碼,如果您看見的是本條內(nèi)容請與安德兔管理員聯(lián)系!',//郵件默認內(nèi)容,當收件人屏蔽了內(nèi)容或某些意外情況時展現(xiàn) ]; public function email($data=[]) { $validate = new Validate([ ['email','require|email','郵箱輸入錯誤|郵箱輸入錯誤'], ['subject','require','請輸入郵件標題'], ['message','require','請輸入郵件內(nèi)容'], ]); 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']; //發(fā)件人地址(也就是你的郵箱地址) $phpmailer->FromName = $config['fromname']; //發(fā)件人姓名 $phpmailer->CharSet = 'utf-8'; //設置郵件編碼 $phpmailer->Subject = $data['subject']; //郵件主題 $phpmailer->Body = $data['message']; //郵件內(nèi)容 $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; }}?>
然后在控制器中調(diào)用模型來實現(xiàn)郵件發(fā)送(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('郵件發(fā)送成功'); } return $this->fetch(); }}最后再創(chuàng)建一個測試模板文件(email.html):
<!doctype html><html lang="zh-CN"><head> <meta charset="utf-8"> <title>郵件發(fā)送測試</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>郵件發(fā)送測試</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">郵件內(nèi)容</label> <div class="col-sm-10"> <textarea class="form-control" row="6" name="message">測試郵件內(nèi)容</textarea> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-success">發(fā)送郵件</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.andetu.com/category/thinkphp案例下載地址:http://www.andetu.com/code/1862