Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Email delivery

PHPMailer breaks on newer PHP because of __autoload() or PHPMailerAutoload.php

Errorrequire 'PHPMailerAutoload.php' in code that worked on an old PHP version

Short answer

PHP 8.0 removed the __autoload() function, and PHPMailer 5.x with its PHPMailerAutoload.php file belongs to that older style. Install PHPMailer 6 or later with Composer, load vendor/autoload.php, use the namespaced classes, and replace any own __autoload() with spl_autoload_register().

  • Applies to PHP 8.3, PHP 8.4, PHPMailer 6, PHPMailer 7
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find out which style your project uses: your own __autoload(), the old PHPMailer 5 includes, or Composer. The search shows which cause below applies.

    grep -rn -E "function __autoload|PHPMailerAutoload|class\.phpmailer|vendor/autoload" --include=*.php .
    php -v
  2. 2Match what you see, then jump to its fix

    1. 1A function __autoload($class) in your own files that is supposed to load PHPMailerYour own code defines __autoload()Fix
    2. 2require 'PHPMailerAutoload.php' or require 'class.phpmailer.php' and require 'class.smtp.php'The project still uses the PHPMailer 5.x includesFix
    3. 3A class-not-found error for PHPMailer\PHPMailer\Exception or SMTP when only PHPMailer.php is includedManual install without Composer is missing one of the filesFix

What does the error look like?

  • require 'PHPMailerAutoload.php' in code that worked on an old PHP version
  • A function named __autoload() in your own bootstrap file
  • new PHPMailer() fails after moving to PHP 8

Cause 1 of 3

Your own code defines __autoload()

1Check

The PHP manual marks __autoload() deprecated since 7.2 and removed in 8.0. The 7.2 notes say it cannot chain autoloaders, so it cannot work next to other libraries either.

grep -rn "function __autoload" --include=*.php .

2Fix

Replace the function with spl_autoload_register() and keep the same loading logic inside the callback.

<?php
spl_autoload_register(function (string $class): void {
    $file = __DIR__ . '/classes/' . str_replace('\\', '/', $class) . '.php';
    if (is_file($file)) {
        require $file;
    }
});

3Verify

Run the script under PHP 8 and confirm the classes load without the old function.

php -l bootstrap.php && php bootstrap.php

Confirmed by php.net

Did this fix it?

Cause 2 of 3

The project still uses the PHPMailer 5.x includes

1Check

PHPMailer 5.2 supported the old PHP versions. The PHPMailer upgrade notes tell you to remove those legacy includes and to use the 6.x namespaced classes. Version 5.2 is no longer supported.

grep -rn -E "PHPMailerAutoload|class\.phpmailer|class\.smtp|phpmailerException" --include=*.php .

2Fix

Install a current release with Composer, load the Composer autoloader, import the classes, and change the exception class.

composer require phpmailer/phpmailer

<?php
require __DIR__ . '/vendor/autoload.php';

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

$mail = new PHPMailer(true);
try {
    // set up and send
} catch (Exception $e) {
    echo $mail->ErrorInfo;
}

3Verify

The script runs with no reference to PHPMailerAutoload.php, and a test message sends.

php send-test.php

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

Manual install without Composer is missing one of the files

1Check

The PHPMailer README lists three files to load for a manual install: Exception.php, PHPMailer.php and SMTP.php from the src folder. Exception.php is needed even when you do not use exceptions, because PHPMailer uses it internally.

ls PHPMailer/src/

2Fix

Require all three files before creating the object, or switch to Composer.

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

3Verify

Create the object and send a test message; no class-not-found error appears.

php send-test.php

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Install libraries with Composer and load only vendor/autoload.php.
  • Run composer outdated now and then so a library never falls years behind the PHP version.
  • Keep a staging copy on the next PHP version and run the mail test there first.
  • Do not define __autoload() in new code; register autoloaders with spl_autoload_register().

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.