composer require phpmailer/phpmailer
代码语言:javascript复制$result = SMTP::getInstance()->send($email,'这里是邮件标题'.time(),'您的验证码是:'.$code);
复制代码
代码语言:javascript复制<?php
/**
* Created by PhpStorm.
* User: Owen Zhang
* Date: 2019/3/19
* Time: 下午2:19
*/
namespace appcommon;
use PHPMailerPHPMailerPHPMailer;
use Log;
class SMTP
{
use InstanceTrait;
/**
* 发送邮件
* @access public
* @param $email 邮箱地址
* @param $subject 邮件标题
* @param $content 邮件内容
* @param $isHtml 是否以HTML文档格式发送 发送后客户端可直接显示对应HTML内容
* @return array
*/
public function send($email, $subject, $content, $isHtml = false)
{
$mail = new PHPMailer;
$mail->isSMTP();
$mail->CharSet = "UTF-8";
$mail->Encoding = 'base64';
$mail->SMTPDebug = config("app_env") == "local" ? 1 : 0; //是否调试
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl'; // 允许 TLS 或者ssl协议
$mail->Host = "smtp.163.com";
// $mail->Port = config("app_env") == "local" ? 465 : 587;
$mail->Port = 465;
$mail->Username = "owen24@163.com";
$mail->Password = "EXVH123456KDSGGRJ";
$mail->setFrom("owen24@163.com", "owenweb 社区"); // 发件人邮箱和名称
$mail->Subject = $subject;
$mail->Body = $content;
$mail->isHTML($isHtml); //是否启用html
$mail->addAddress($email); //收件用户
$mail->send();
if (!$mail->send()) {
var_dump($content);
var_dump($mail->ErrorInfo);
// Log::warning("邮件发送失败: " . $mail->ErrorInfo, []);
return ["code" => _MSG_SYSTEM_ERROR, "msg" => "邮件发送失败"];
}
return ["code" => _MSG_SYSTEM_SUCCESS, "msg" => "邮件发送成功"];
}
}
复制代码
163邮箱SMTP设置
开启后就会显示一串密码,即是$mail->Password = "EXVH123456KDSGGRJ";修改即可