致命错误: Class ‘PHPMailer\PHPMailer\PHPMailer‘ not found 如何解决

发布于:2024-06-19 ⋅ 阅读:(139) ⋅ 点赞:(0)

引用发邮箱的类总数报错,方法是在gitee上下载对应的这个目录的,代码放在对应的目录下引用,就可以了,下载地址是 :源代码以及phpmailer下载:
https://gitee.com/chenkk0613/email,以下是参考代码,需要在发送的邮箱开启授权码,然后复制到这里

<?php

namespace app\common\server;

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';


class EmsServer
{
    public function sendMail($to, $subject, $content)
    {
        //$mail = new PHPMailer(true);
        $mail = new PHPMailer(true);
        //$config = Config::get('mail');
        $config = [ 'host'=>'smtphz.qiye.163.com',
                    'port'=>465,
                    'username'=>'o2323@1688order.com',
                    'password'=>'111111',
                    'mail_from'=>'2222l@1688order.com', // 发件人邮箱
                    'name_from'=>'1688order',// 发件人名称
                  ];

        $mail->isSMTP(); // 使用SMTP服务发送邮件
        $mail->SMTPAuth = true; // 启用 SMTP 认证
        $mail->Host = $config['host']; // SMTP 服务器
        $mail->Port = $config['port']; // SMTP服务器的端口号
        $mail->SMTPSecure = 'ssl'; // SMTP服务器的端口号
        $mail->Username = $config['username']; // SMTP账号
        $mail->Password = $config['password']; // SMTP密码

        $mail->From = $config['mail_from']; // 发件人邮箱
        $mail->FromName = $config['name_from']; // 发件人名称
        $mail->isHTML(true); // 邮件正文是否为html编码
        $mail->CharSet = 'utf-8'; // 设置邮件字符集
        $mail->addAddress($to); // 收件人邮箱地址
        $mail->Subject = $subject; // 邮件标题
        $mail->Body = $content; // 邮件内容

        if (!$mail->send()) {
            return $mail->ErrorInfo;
        } else {
            return true;
        }
    }
}