首页 > 解决方案 > 致命错误:未捕获的错误:在 D:\projects\cscpl\PHPMailer.php 中找不到类 'PHPMailer\PHPMailer\SMTP'

问题描述

我在没有作曲家的情况下手动安装了 PHPMailer,并在 cscpl 文件夹中添加了五个文件。我正在从 gmail 发送邮件。但我收到以下错误。

Fatal error: Uncaught Error: Class 'PHPMailer\PHPMailer\SMTP' not found in 
D:\projects\cscpl\PHPMailer.php:1824 Stack trace: #0 D:\projects\cscpl\PHPMailer.php(1945): 
PHPMailer\PHPMailer\PHPMailer->getSMTPInstance() #1 D:\projects\cscpl\PHPMailer.php(1861): 
PHPMailer\PHPMailer\PHPMailer->smtpConnect(Array) #2 D:\projects\cscpl\PHPMailer.php(1604):
PHPMailer\PHPMailer\PHPMailer->smtpSend('Date: Thu, 17 S...', 'This is a multi...') #3
D:\projects\cscpl\PHPMailer.php(1436): PHPMailer\PHPMailer\PHPMailer->postSend() #4 
D:\softwares\installed\xampp\htdocs\contact.php(91): PHPMailer\PHPMailer\PHPMailer->send() 
#5 {main} thrown in D:\projects\cscpl\PHPMailer.php on line 1820

在 cscpl 文件夹中,我有一个包含以下内容的 contact.php 文件

<?php
    require_once('config.php');
    use PHPMailer\PHPMailer\PHPMailer;
    // use PHPMailer\PHPMailer\Exception;


    // require_once('D:\projects\cscpl\Exception.php');
    require_once('D:\projects\cscpl\PHPMailer.php');
    require_once('D:\projects\cscpl\SMTP.php');

    $subject = "This is subject";
    $msg = "This is message";
        try{
            echo 'trying';
            $php_mailer = new PHPMailer(true);
            $php_mailer->isSMTP();
            $php_mailer->Host = 'smtp.gmail.com';
            $php_mailer->SMTPAuth = true;
            $php_mailer->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
            $php_mailer->Port = 587;
            $php_mailer->Username = 'sender_email@gmail.com';
            $php_mailer->Password = 'sender_pwd';
            $php_mailer->setFrom('sender_email@gmail.com');
            $php_mailer->addAddress('receiver_email@gmail.com');
            $php_mailer->IsHTML(true);
            $php_mailer->Subject = $subject;
            $php_mailer->Body = 'HTML message body. <b>Gmail</b>'.$msg.' .';
            $php_mailer->send();
            echo 'done';
        }
        catch (Exception $e)
        {
            echo 'exp';
            /* PHPMailer exception. */
            echo $e->errorMessage();
            print_r(error_get_last());
        }

在 PHPMailer.php 的第 1824 行,它显示

   public function getSMTPInstance()
    {
        if (!is_object($this->smtp)) {
            $this->smtp = new SMTP();
        }

        return $this->smtp;
    }

smtp.php 文件看起来像这样

<?php
/**
 * PHPMailer Exception class.
 * PHP Version 5.5.
 *
 * @see       https://github.com/PHPMailer/PHPMailer/ The PHPMailer GitHub project
 *
 * @author    Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
 * @author    Jim Jagielski (jimjag) <jimjag@gmail.com>
 * @author    Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
 * @author    Brent R. Matzelle (original founder)
 * @copyright 2012 - 2020 Marcus Bointon
 * @copyright 2010 - 2012 Jim Jagielski
 * @copyright 2004 - 2009 Andy Prevost
 * @license   http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
 * @note      This program is distributed in the hope that it will be useful - WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 */

namespace PHPMailer\PHPMailer;

/**
 * PHPMailer exception handler.
 *
 * @author Marcus Bointon <phpmailer@synchromedia.co.uk>
 */
class Exception extends \Exception
{
    /**
     * Prettify error message output.
     *
     * @return string
     */
    public function errorMessage()
    {
        return '<strong>' . htmlspecialchars($this->getMessage()) . "</strong><br />\n";
    }
}

我不知道我做错了什么。

标签: phpemailphpmailer

解决方案


您显示的文件内容SMTP.php实际上是存在的内容Exception.php

这就是为什么您require_once('D:\projects\cscpl\SMTP.php');没有失败,但随后尝试创建 SMTP 类的实例却失败了。

使用正确的名称重新安装文件!

您真的根本不必触摸库文件;作曲家会为您处理所有这些。


推荐阅读