目录
1、使用composer 工具安装,在tp5根目录下执行
2、封装发送邮件方法
3、控制器中调用
4、运行后结果
1、使用composer 工具安装,在tp5根目录下执行
composer require phpmailer/phpmailer
安装成功后显示下面目录
2、封装发送邮件方法
function sendEmail($to, $title, $content){$email=["send_email"=>"231323@qq.com","host"=>"smtp.qq.com","pwd"=>"vrgtobnx2232aogugaaf","encry_type"=> "ssl","port"=>465,"send_name"=>"测试","title"=>"QQ邮箱"];try {$mail = new PHPMailer\PHPMailer\PHPMailer(true);$mail->isSMTP();$mail->Host = $email['host']; //qq邮箱的服务器地址$mail->SMTPAuth = true;$mail->Username = $email['send_email'];//授权的qq邮箱$mail->Password = $email['pwd'];//授权码,不是密码!!!$mail->SMTPSecure = $email['encry_type'];// 使用 ssl 加密方式登录boolean$mail->Port = $email['port'];//smtp 服务器的远程服务器端口号//Recipients$mail->setFrom($email['send_email'], $email['send_name']);//授权的qq邮箱(和上面一样),自己起的昵称$mail->addAddress($to); // 传过来的收件人$mail->isHTML(true); // Set email format to HTML$mail->Subject = $title;//传过来的标题$mail->Body = $content;//传过来的内容if($mail->send()){return return_msg();}else{return return_msg(-1,"error");}} catch (\PHPMailer\PHPMailer\Exception $e) {return return_msg(-1,$mail->ErrorInfo);}
}function return_msg($status=1,$msg="ok",$data=[]){return ["status" => $status,"msg" => $msg,"data" => $data,];}
3、控制器中调用
public function index(){$str="这是测试内容";$title="这是测试邮件";$sendEmail="123@qq.com";//接受人$res=sendEmail($sendEmail,$title,$str);var_dump($res);exit();}
4、运行后结果