1.安装composer
2.设置composer为全局变量
3.进入项目目录
4.在命令行执行:composer require phpmailer/phpmailer
5.创建Test.controller控制器文件,代码如下:
<?php
/**
* Created by PhpStorm.
* User: dyane
* Date: 2019/3/29
* Time: 16:38
*/
namespace app\system_admin\controller;
use think\Request;
class Test
{
/**
* 邮件发送页面
* @return mixed
*/
public function index()
{
return view('email/index');
}
/**
* 发送邮件
*/
public function send()
{
if($this->request->isPost()) {
$param = $this->request->param();
//判断收件人
if (empty($param['addressee'])) {
return json(['code' => 400, 'msg' => '收件人地址不能为空!']);
die;
}
//判断邮件主题和内容必须存在一个
if (empty($param['title']) && empty($param['content'])) {
return json(['code' => 400, 'msg' => '主题和内容必须填写一个!']);
die;
}
//获取收件人地址
$addressee = $param['addressee'];
//验证邮箱
$addressee = $this->checkEmailStr($addressee);
//获取主题或内容
if (!empty($param['title'])) {
$title = htmlspecialchars($param['title']);
} else {
$title = ' ';
}//空格不能去,否则发送失败
if (!empty($param['content'])) {
$content = $param['content'];
} else {
$content = ' ';
}//空格不能去,否则发送失败
//获取抄送人地址
if (!empty($param['copyperson'])) {
$copyerArray = $this->checkEmailStr($param['copyperson']);
} else {
$copyerArray = '';
}
//获取密送人地址
if (!empty($param['secretagent'])) {
$sercreterArray = $this->checkEmailStr($param['secretagent']);
} else {
$sercreterArray = '';
}
//获取附件
if (!empty($param['files_list'])) {
$filesArray = $this->checkFiles($param['files_list']);
} else {
$filesArray = '';
}
$res = $this->sendEmail($addressee,$copyerArray,$sercreterArray,$filesArray,$title,$content);
//反馈发送结果
if($res){
if (!empty($filesArray)) {
$this->deleteOldEmaileFiles($filesArray);
}
echo json_encode(['code' => 200, 'msg' => '发送成功!']);die;
}else{
echo json_encode(['code' => 400, 'msg' => '发送失败!']);die;
}
}
}
/**
* 发送邮件
* @param $addressee
* @param $copyerArray
* @param $sercreterArray
* @param $filesArray
* @param $title
* @param $content
*/
protected function sendEmail($addressee,$copyerArray,$sercreterArray,$filesArray,$title,$content)
{
//使用发送邮件类
$mail = new EmailYX();
//设置收件人
$mail->setManyAddressee($addressee);
if(!empty($copyerArray)){$mail->setManyCC($copyerArray);}
if(!empty($sercreterArray)){$mail->setManyBCC($sercreterArray);}
if(!empty($filesArray)){$mail->setManyAttachment($filesArray);}
//设置主题和内容
$mail->setContent($title,$content);
//发送邮件
$res = $mail->send();
return $res;
}
/**
* 发送成功之后删除附件文件
* @param $filesArray
*/
protected function deleteOldEmaileFiles($filesArray)
{
foreach ($filesArray as $key => $value){
$path = ROOT_PATH . 'public'.$value;
if(file_exists($path)){
unlink($path);
}
}
}
/**
* 验证邮箱是否正确
* @param $str
*/
protected function checkEmailStr($emailStr)
{
$toArray = explode(',',$emailStr);
$res = $this->checkManyEmail($toArray);
return $res;
}
/**
* 判断多组字符窜邮箱是否有不正确的邮箱存在
* @param $toArray
* @return bool
*/
protected function checkManyEmail($toArray)
{
foreach ($toArray as $key => $value){
$check = $this->checkEmail($value);
if(!$check){unset($toArray[$key]);}
}
return $toArray;
}
/**
* 正则验证邮箱是否合法
* @param $email
* @return false|int
*/
protected function checkEmail($email)
{
// $pregEma