The major things that have changed in PHPMailer 6.0:
- Requires PHP 5.5 or later (up from 5.0)
- Uses a namespace
- Class filenames and locations have changed
- Additional unrelated "extras" classes have been removed
There are many other smaller changes which you can read about in the changelog, and also the official upgrade guide, but these are the ones most likely to affect you.
Upgrading via composer
To upgrade via composer, change the entry in the require
section of your composer.json
file, and then run composer update phpmailer/phpmailer
:
"phpmailer/phpmailer": "~6.0"
This will update only PHPMailer, and will not touch any other dependencies.
PHPMailer uses a semver release numbering policy, and that pattern will match all future releases in the 6.x series. This is a change from the previously recommended ~5.2
pattern.
Loading class files
For the example script given, we mainly need to change how the class is loaded. The autoloader is no longer there, so you either need to be using composer (in which case you won't need to change anything - the standard composer autoloader will do it automatically), or you need to load the classes yourself.
With composer:
require 'vendor/autoload.php';
Without composer:
require 'src/PHPMailer.php';
require 'src/SMTP.php';
require 'src/Exception.php';
Namespacing
The PHPMailer classes are in the PHPMailerPHPMailer
namespace, so you either need to work in that namespace, or import them into your own or the global namespace, for example:
//Import PHPMailer classes into the global namespace
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;
use PHPMailerPHPMailerException;
Note that these must be placed before the require
lines. After that you can use the original class names you're used to:
$mail = new PHPMailer;
Alternatively, you can refer to their fully-qualified names directly, without the use
statements, for example:
$mail = new PHPMailerPHPMailerPHPMailer;
The reason this class ends up with this "triple name" is because it's the PHPMailer class, in the PHPMailer project, owned by the PHPMailer organisation. This allows it to be differentiated from other forks of PHPMailer, other projects by the PHPMailer organisation, and other classes within the project.
Exceptions
Other than the name change from phpmailerException
, exceptions work the same way as in previous versions, but you need to look out for the namespace when catching:
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;
use PHPMailerPHPMailerException;
$mail = new PHPMailer(true);
try {
...
} catch Exception($e) {
//$e is an instance of PHPMailerPHPMailerException
} catch Exception ($e) {
//$e is an instance of the PHP built-in Exception class
}
Documentation
All documentation and example code has been updated for 6.0 too. The best place to start is the readme file or the project wiki, where you will find links to the ever-popular troubleshooting guide, numerous tutorials, and generated API documentation. If you're just starting out, base your code on the examples provided in the examples folder.
Getting help
If you have a problem using PHPMailer, first of all search on Stack Overflow for your specific error message and under the PHPMailer tag. If you think you have found a bug in PHPMailer, report it on the github project (hint - being unable to connect to mail servers from your GoDaddy server is not a PHPMailer bug!)