PHPMailer安装与配置简单说明
2019-08-26 阅读 : 次
1. 通过composer安装PHPMailer
composer require phpmailer/phpmailer
2. html页面
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>首页</title>
</head>
<body>
<div class="container-fluid">
<div class="row-fluid">
<div class="span4">
</div>
<div class="span4">
<form class="form-horizontal" method="post" action="{:url('index/index')}">
<div class="control-group">
<label class="control-label" for="inputEmail">邮箱</label>
<div class="controls">
<input id="inputEmail" type="text" name="user_email"/>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputPassword">类容</label>
<div class="controls">
<input id="inputPassword" type="text" name="content" />
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" class="btn">发送</button>
</div>
</div>
</form>
</div>
<div class="span4">
</div>
</div>
</div>
</body>
</html>
3. 配置邮箱并开启POP3/SMTP/IMAP服务(以163邮箱为例)
4. PHP控制器
<?php
namespace app\index\controller;
use think\Controller;
use PHPMailer\PHPMailer\PHPMailer;
use think\Request;
class Index extends Controller
{
public function index()
{
if (request()->isPost()){
$re = input('post.');
$data = [
'user_email' => $re['user_email'], //接收人邮箱
'content' => $re['content']
];
$this->sendEmail($data);
}else{
return view();
}
}
public function sendEmail($data = []) {
$mail = new phpmailer(); //实例化
$mail->IsSMTP(); // 启用SMTP
$mail->Host = 'smtp.163.com'; //SMTP服务器 以qq邮箱为例子
$mail->Port = 465; //邮件发送端口
$mail->SMTPAuth = true; //启用SMTP认证
$mail->SMTPSecure = "ssl"; // 设置安全验证方式为ssl
$mail->CharSet = "UTF-8"; //字符集
$mail->Encoding = "base64"; //编码方式
$mail->Username = '1529*****927@163.com'; //发件人邮箱
$mail->Password = '*****'; //发件人密码 ==>重点:是{授权码}邮箱设置里面,不是邮箱密码
$mail->Subject = '邮箱验证'; //邮件标题
$mail->From = '152******927@163.com'; //发件人邮箱
$mail->FromName = '测试'; //发件人姓名
if($data && is_array($data)){
$mail->AddAddress($data['user_email']); //添加收件人
$mail->IsHTML(true); //支持html格式内容
$mail->Body = $data['content']; //邮件主体内容
//发送成功就删除
if ($mail->Send()) {
//echo "Mailer Error: ".$mail->ErrorInfo;// 输出错误信息,用以邮件发送不成功问题排查
return $this->success('发送成功');
}else{
return $this->error('发送失败');
}
}
}
}
作者:_Damon
本文地址:https://www.helloaliyun.com/tutorial/123.html