前期准备
1.获取开源的phpmail类
链接:https://pan.baidu.com/s/1mabENEdQal8_qZ5gh5ZtGA
提取码:4fb4
2.开启stmp服务
3.开启授权码
注意密码一定要记住
4.把下载的phpmail类放到tp5中的extend目录中
5.配置Email.php
<?php
/**
* 发送邮件类库
*/
namespace phpmailer;
use think\Exception;
class Email {
/**
* @param $to
* @param $title
* @param $content
* @return bool
*/
public static function send($to, $title, $content) {
date_default_timezone_set('PRC');//set time
if(empty($to)) {
return false;
}
try {
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
// $mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = config('email.host'); //SMTP服务器: smtp.163.com
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = config('email.port'); //端口号
$mail->SMTPAuth = true;
$mail->Username = config('email.username'); //邮箱号
$mail->Password =config('email.password'); //授权码
$mail->setFrom(config('email.username'), '官网');
$mail->addAddress($to); //要发送的邮箱号
$mail->Subject = $title; //发送标题
$mail->msgHTML($content); //发送内容
//send the message, check for errors
if (!$mail->send()) {
return false;
} else {
return true;
}
}catch(phpmailerException $e) { //抛出异常
return false;
}
}
}
6.在application目录下自己定义一个发送邮件的配置文件
<?php
/**
*发送邮件相关配置
*/
return [
'host' => "smtp.163.com", //SMTP服务器: smtp.163.com
'port' => 25, //端口
'username' => "自己写", //邮箱号
'password' => '自己写', //授权码
];
7.在需要的控制器中调用
\phpmailer\Email::send('邮箱号','标题','内容'); return '成功';
如果视图中输出成功,那么邮箱发送成功