如何修复 PHPMailer __autoload() 错误?

mAh*_*iki 6 php phpmailer

我使用 phpMailer 来处理从网站发送的邮件。今天早上我突然收到这样的消息:

致命错误:不再支持 __autoload(),请在第 45 行的 C:\xampp\htdocs\webapp\PHPMailerAutoload.php 中使用 spl_autoload_register() 代替

我的服务器上运行的是 PHP 8.0.0

if (version_compare(PHP_VERSION, '5.1.2', '>=')) {
    //SPL autoloading was introduced in PHP 5.1.2
    if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
        spl_autoload_register('PHPMailerAutoload', true, true);
    } else {
        spl_autoload_register('PHPMailerAutoload');
    }
} else {
    /**
     * Fall back to traditional autoload for old PHP versions
     * @param string $classname The name of the class to load
     */
    function __autoload($classname)
    {
        PHPMailerAutoload($classname);
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 10

这是编辑后的自动加载文件:PHPMailer\PHPMailerAutoload.php

function PHPMailerAutoload($classname)
{
    //Can't use __DIR__ as it's only in PHP 5.3+
    $filename = dirname(__FILE__).DIRECTORY_SEPARATOR.'class.'.strtolower($classname).'.php';
    if (is_readable($filename)) {
        require $filename;
    }
}

if (version_compare(PHP_VERSION, '5.1.2', '>=')) {
    //SPL autoloading was introduced in PHP 5.1.2
    if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
        spl_autoload_register('PHPMailerAutoload', true, true);
    } else {
        spl_autoload_register('PHPMailerAutoload');
    }
} else {
    /**
     * Fall back to traditional autoload for old PHP versions
     * @param string $classname The name of the class to load
     */
    spl_autoload_register($classname);
    
}
Run Code Online (Sandbox Code Playgroud)

更改在这里完成: spl_autoload_register($classname);

谢谢


Syn*_*hro 6

您正在使用 PHPMailer \xe2\x80\x93 的非常旧版本,该代码已经 3 年没有出现在 PHPMailer 中了。从PHPMailer 6.2.0开始正式支持 PHP 8.0 ,并确保您阅读升级指南。

\n