How to Upgrade from PHPmailer 5.2 to 6.0

How should I upgrade from PHPMailer 5.2 to 6.0?

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 PHPMailer\PHPMailer 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 PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

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 PHPMailer\PHPMailer\PHPMailer;

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 PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

$mail = new PHPMailer(true);
try {
...
} catch Exception($e) {
//$e is an instance of PHPMailer\PHPMailer\Exception
} 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!)

PHPMailer Classes - How to download the latest stable version from GitHub without Composer

This is a question for me!

The definitive place to look for answers on upgrading PHPMailer from 5 to 6 is the UPGRADING doc, and also this question which I wrote specifically for this situation.

Yes, those three files are all you need for basic mail sending. Composer only becomes more significant when you use Oauth, which introduces numerous dependencies.

With hindsight I should have called the stable branch legacy to make it clearer that it’s deprecated. All versions of PHP 5 are now end-of-life, so nobody should be using that branch any more.

The version properties were changed to constants in 6 because they are obviously constant! TBH they mainly serve to be annoying during releases. The main classes and the VERSION file are the only ones which contain a version string now (and they do all contain one?). For the most part version numbers should only be of interest to composer. The exception class just isn’t interesting enough to warrant one.

It’s true that the HEAD of the master branch may be unstable, but tagged releases come from there and should be considered stable. Again, this happens automatically if you use composer. I don't recommend deploying versions directly from HEAD, but again, this is why you should use composer. Even if you don't want to run composer at installation time, or can't run it when deploying (e.g. your users are on shared hosting), you should use it when you build your installation bundle locally so that composer can take care of it for you.

If you've not figured it out from this, using composer really is the path of least resistance, and takes away a lot of complexity; you'll probably never need to write another include/require statement again.

How to update PHPMailer and check installed version

It depends on how you installed it. If you're using the recommended method of using composer, then a simple composer update will get you the latest version. Your composer.lock file will show you which version you currently have, or you can open the VERSION file which will show you the version number you've got - or if you have a very old version that predates the VERSION file, look in the source files instead - they will also say what version they are. If you've installed it manually, just download the latest version from Github and replace your current version.

PHPMailer Update Issue (from 5.2.9 to 5.2.16)

After spending a day or so asking various related questions, I found the answer lies with an update applied in PHPMailer 5.2.10.

I found that while my server is TLS certified, there seems to be a problem with using a localhost Host; possibly that PHPMailer may be trying to check the certificate applies to localhost domain, which of course it doesn't.

I found a solution to sending mail via Localhost now is therefore to disable auto-TLS with the following:

    $mail->SMTPAutoTLS = false; 

Using this all emails now send correctly.


NOTE

As Bodi0 stated using SMTPAuth is best practise, however I didn't realise that it is unnessecary for localhost mail servers as the authenticaton is implicit.

Comments on this answer to a related question about ->SMTPSecure settings gives more details.

How to force TLS 1.2 usage for PhpMailer 5.2

It's not up to PHPMailer, its up to the version of PHP that you're using to run it, so the solution is to update your PHP version. The major changes relating to TLS were largely in PHP 5.6, so upgrading to that would be a good intermediate point if you're really stuck with this legacy version.

Im using php version 5.2. The script is working in 000webhost, but when i use yahoo web host it doesnt work

The error is clear on your post.

You have a "no route to host", so, the problem are your web hosting have blocked you to connect to gmail servers.



Related Topics



Leave a reply



Submit