类库名称为:phpmailer/phpmailer
public function sendSimpleEmail($email, $title, $content)
{
$mailConfig = config("email");
$mail = new PHPMailer(); // 实例化
$mail->IsSMTP(); // 启用SMTP
$mail->Host = $mailConfig['MAIL_HOST']; // smtp服务器的名称(这里以QQ邮箱为例)
$mail->SMTPAuth = $mailConfig['MAIL_SMTPAUTH']; // 启用smtp认证
$mail->Username = $mailConfig['MAIL_USERNAME']; // 你的邮箱名
$mail->Password = $mailConfig['MAIL_PASSWORD']; // 邮箱密码
$mail->From = $mailConfig['MAIL_FROM']; // 发件人地址(也就是你的邮箱地址)
$mail->FromName = $mailConfig['MAIL_SEND_NAME']; // 发件人姓名
$mail->AddAddress($email, "尊敬的客户");
$mail->WordWrap = 50; // 设置每行字符长度
$mail->IsHTML(true); // 是否HTML格式邮件
$mail->CharSet = $mailConfig['MAIL_CHARSET']; // 设置邮件编码
$mail->Subject = $title; // 邮件主题
$mail->Body = $content; // 邮件内容
// $mail->AltBody = "这是一个纯文本的身体在非营利的HTML电子邮件客户端"; //邮件正文不支持HTML的备用显示
$bool = $mail->Send();
if ($bool) {
$data ["status"] = 1;
$data ["msg"] = "邮件已送到您的邮箱";
} else {
$data ["status"] = 2;
$data ["msg"] = "发送失败";
}
return $data;
}
PHP邮件类的配置文件
return [
"MAIL_SERVER_HOST"=>"http://www.solo90.com", //项目主机域名
'MAIL_HOST' =>'smtp.163.com',//smtp服务器的名称
'MAIL_SMTPAUTH' =>TRUE, //启用smtp认证
'MAIL_USERNAME' =>'simpleplan5@163.com',//你的邮箱名
'MAIL_FROM' =>'simpleplan5@163.com',//发件人地址
'MAIL_FROMNAME'=>'管理员',//发件人姓名
'MAIL_PASSWORD' =>'123123',//邮箱密码
'MAIL_CHARSET' =>'utf-8',//设置邮件编码
'MAIL_ISHTML' =>TRUE, // 是否HTML格式邮件
'MAIL_SEND_NAME'=>"管理员",//发送者名称
'MAIL_SEND_TITLE_active'=>"XX网邮箱激活",//发送激活邮件时候的标题
'MAIL_SEND_TITLE_sendcode'=>"XX网邮件验证码",//发送邮件验证码时候的标题
'MAIL_SEND_JIANGE'=>"1",//邮件发送间隔,单位秒
'MAIL_SEND_SHIXIAO'=>"1800",//邮件uuid生效时间
];
我在项目中的调用方法
$serverHost=config("email.MAIL_SERVER_HOST");
$contentLink=$serverHost."/callback/email?token=".$token;
$title="xxxx邮箱变更通知";
$this->assign('url',$serverHost);
$this->assign('link',$contentLink);
$this->assign('email',$email);
$content=$this->fetch('index/email_template');
$result=$this->sendSimpleEmail($email,$title,$content);
die(json_encode($result));