ThinkPHP 通过 PHPMailer插件来发送邮件

1、修改 application 中的 common.php 文件

use PHPMailer\PHPMailer\PHPMailer;

function email($mailto, $subject, $content)
{
    // 实列化PHPMailer,同时传递true表示启用异常机制
    $mail = new PHPMailer(true);
    try {
        $mail->SMTPDebug = 0;                                 // 启用调试
        $mail->isSMTP();                                      // 设置mailer使用简单的邮件传输协议
        $mail->Host = 'smtp.163.com';                         // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = '邮箱名';                   // SMTP username
        $mail->Password = '密码';                         // SMTP password
        $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 465;                                    // TCP port to connect to
        $mail->CharSet = 'utf-8';

        //Recipients
        $mail->setFrom('[email protected]', 'GetcharZp');
        $mail->addAddress($mailto);                           // 发送到的目标邮箱
        //Content
        $mail->isHTML(true);                           // Set email format to HTML
        $mail->Subject = $subject;                            // 发送邮箱的标题
        $mail->Body    = $content;                            // 发送邮箱的正文

        return $mail->send();
    }catch (Exception $e) {
        exception($mail->ErrorInfo, 1001);
    }
}

2、在 Controller 和 Modal 中都阔以直接使用 email 方法了

猜你喜欢

转载自www.cnblogs.com/GetcharZp/p/12368293.html
今日推荐