企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
| 基于TP5发送邮件 | | | --- | --- | ``` <?php namespace app\client\controller; use think\Controller; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; class SendMail extends Controller { /** * [index 方法一,发送邮件] * @return [type] [description] */ public function index() { $to_user = [ '95633794@qq.com', '95633794@qq.com', ]; $subject = '邮件的标题'; $content = '邮件的内容</br><h2>sb</h2>'; $this->send($to_user, $subject, $content); } /** * [send 方法一,发送邮件] * @param [array] $to_user [邮件接收人,可多个] * @param string $subject [邮件的标题] * @param string $content [邮件的内容] * @return [type] [description] */ private function send($to_user,$subject='',$content='') { // 实例化PHPMailer核心类 $mail = new PHPMailer(); // 是否启用smtp的debug进行调试 开发环境建议开启 生产环境注释掉即可 默认关闭debug调试模式 $mail->SMTPDebug = 1; // 使用smtp鉴权方式发送邮件 $mail->isSMTP(); // smtp需要鉴权 这个必须是true $mail->SMTPAuth = true; // 链接qq域名邮箱的服务器地址 $mail->Host = 'smtp.qq.com'; // 设置使用ssl加密方式登录鉴权 $mail->SMTPSecure = 'ssl'; $mail->SMTPOptions = [ 'ssl' => [ 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true ] ]; // 设置ssl连接smtp服务器的远程服务器端口号 $mail->Port = 465; // 设置发送的邮件的编码 $mail->CharSet = 'UTF-8'; // 设置发件人昵称 显示在收件人邮件的发件人邮箱地址前的发件人姓名 $mail->FromName = '发件人昵称'; // smtp登录的账号 QQ邮箱即可 $mail->Username = '247958201@qq.com'; // smtp登录的密码 使用生成的授权码 $mail->Password = 'fppbaycxgaomcaec'; // 设置发件人邮箱地址 同登录账号 $mail->From = '247958201@qq.com'; // 邮件正文是否为html编码 注意此处是一个方法 $mail->isHTML(true); // 设置收件人邮箱地址 if (is_array($to_user)) { foreach ($to_user as $key => $val) { $mail->addAddress($val); } } // 添加多个收件人 则多次调用方法即可 $mail->addAddress('906311934@qq.com'); // 添加该邮件的主题 $mail->Subject = $subject; // 添加邮件正文 $mail->Body = $content; // 为该邮件添加附件 $mail->addAttachment(''); // 发送邮件 返回状态 $status = $mail->send(); return $status; } /** * [sendMail 方法二,发送邮件] * @return [type] [description] */ public function sendMail() { ini_set("'memory_limit", '512M'); $mail = new PHPMailer(true); try { $mail->SMTPDebug = 0; $mail->isSMTP(); $mail->CharSet = 'UTF-8'; $mail->Host = 'smtp.qq.com', $mail->SMTPAuth = true; $mail->Username = '247958201@qq.com'; $mail->Password = 'fppbaycxgaomcaec'; $mail= $this->openSslForPhpMailer($mail); $mail->SMTPSecure = 'ssl'; $mail->Port = 465; $mail->setFrom('247958201@qq.com', 'GYL系统邮件'); $mail->addAddress('956333794@qq.com'); $mail->addAddress('936764458@qq.com'); $mail->addCC('956333794@qq.com'); $mail->addCC('936764458@qq.com'); $mail->addBCC('956333794@qq.com'); $mail->addBCC('936764458@qq.com'); $mail->addReplyTo('247958201@qq.com', 'GYL'); $mail->addAttachment('文件url'); $mail->isHTML(true); $mail->Subject = '标题'; $mail->Body = '内容'; $res = $mail->send(); if ($res) { //写入日志 } }catch (Exception $e) { return $e->ErrorInfo; } } /** * [openSslForPhpMailer 开启ssl] * @param [type] $PHPMailer [description] * @return [type] [description] */ private function openSslForPhpMailer($PHPMailer) { $phpMailer->SMTPOptions = [ 'ssl' => [ 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true ] ]; return $phpMailer; } } ```