5

I use phpMailer for the processing of mail sent from the website. This morning I suddenly got the following message:

Fatal error: __autoload() is no longer supported, use spl_autoload_register() instead in C:\xampp\htdocs\webapp\PHPMailerAutoload.php on line 45

I have PHP 8.0.0 running on the server

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);
    }
}
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
mAhmedSiddiki
  • 113
  • 1
  • 2
  • 7
  • 1
    Did you check all your code would work with PHP8 before upgrading? – RiggsFolly Dec 17 '20 at 18:29
  • I know I'm late to the party, but this answer really helped me a lot https://stackoverflow.com/questions/45940509/how-should-i-upgrade-from-phpmailer-5-2-to-6-0 – Narxx Sep 28 '21 at 18:00

3 Answers3

9

This is An Edited AutoLoad File in: 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);
    
}

Changes are done here: spl_autoload_register($classname);

Thanks

6

You're using a very old version of PHPMailer – that code has not been in PHPMailer for 3 years. PHP 8.0 is officially supported as of PHPMailer 6.2.0, and make sure you read the upgrade guide.

Synchro
  • 35,538
  • 15
  • 81
  • 104
-1

Your issue will be resolved by changing function __autoload($classname) to function spl_autoload_register($classname) which has no adverse affect on the output.

Muna
  • 11
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – jasie Sep 29 '22 at 10:33