PHPMailer breaks on newer PHP because of __autoload() or PHPMailerAutoload.php
require 'PHPMailerAutoload.php' in code that worked on an old PHP versionShort 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().
Fix it fast
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 -v2Match what you see, then jump to its fix
- 1
A function __autoload($class) in your own files that is supposed to load PHPMailerYour own code defines __autoload()Fix - 2
require 'PHPMailerAutoload.php' or require 'class.phpmailer.php' and require 'class.smtp.php'The project still uses the PHPMailer 5.x includesFix - 3
A 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
- 1
What does the error look like?
require 'PHPMailerAutoload.php' in code that worked on an old PHP versionA function named __autoload() in your own bootstrap filenew 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.phpConfirmed by php.net
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.phpConfirmed by raw.githubusercontent.com
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.phpConfirmed by raw.githubusercontent.com
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
Back end
require(vendor/autoload.php): Failed to open stream: No such file or directory
Fatal error: Uncaught Error: Failed opening required 'vendor/autoload.php'
PHP looked for vendor/autoload.php at a path where it does not exist.
3 causes, each with a fixCodeIgniter email does not send and shows no error message
$email->printDebugger() prints an empty result
CodeIgniter signals a failed email only by send() returning false, with no exception.
4 causes, each with a fixLaravel tries to send mail to mailpit:1025 instead of your real SMTP server
Symfony\Component\Mailer\Exception\TransportException
Laravel is still using the development mail host from the starter .env, mailpit on port 1025, which only exists inside Laravel Sail.
3 causes, each with a fixLaravel mail fails because an address does not comply with RFC 2822
Email "Jane Doe <jane@example.com>" does not comply with addr-spec of RFC 2822.
Symfony Mailer checks every address when the Address object is created.
3 causes, each with a fixLaravel says the email was sent, but nothing arrives
Mail::to($user)->send() returns normally
Mail::send returns without an error when the message was accepted by the configured mailer, not when it was delivered.
3 causes, each with a fixLaravel mail fails with 'Unable to connect with STARTTLS' and SSL operation failed with code 1
stream_socket_enable_crypto(): Peer certificate verification failed
The mail server offered STARTTLS, but PHP could not verify the certificate it presented.
3 causes, each with a fix