Jump to content
SaranacLake

Update PHPMailer?

Recommended Posts

I have been away from a coding project for over 2 years.  Trying to get things online, and not cause further delays.  My site uses the PHP Mailer class to send out emails to users for things like password resets.

How important is it that I have the latest version of this class - assuming it is even still around now?

Thanks.

Share this post


Link to post
Share on other sites

PHPMailer is definitely still around. It's one of the top email libraries.

You don't really have to be updating every time. It's more of a question about whether you need what the new versions provide - PHP support, features, that kind of thing - and whether it fixes bugs you should care about. So while you don't have to, you should try to stay fairly current.

Share this post


Link to post
Share on other sites
11 hours ago, requinix said:

PHPMailer is definitely still around. It's one of the top email libraries.

You don't really have to be updating every time. It's more of a question about whether you need what the new versions provide - PHP support, features, that kind of thing - and whether it fixes bugs you should care about. So while you don't have to, you should try to stay fairly current.

 

After migrating my code to a new Mac with a newer version of MAMP, when I getting back into things, I go this error last night...

	Deprecated: ___autoload() is deprecated, use spl_autoload_register() instead in /path/to/PHPMailerAutoload.php on line 45
	

 

I guess that means the newer version of PHP that comes with the newer version of MAMP I am running on my new Mac broke things, huh?

 

********

P.S. How come I didn't get an email notifying me that you responded, when "Notify me of replies" was checked?

Share this post


Link to post
Share on other sites
Just now, SaranacLake said:

I guess that means the newer version of PHP that comes with the newer version of MAMP I am running on my new Mac broke things, huh?

Not broken. "Deprecated" means it works for the time being but will not in the future.

However in this case that's not entirely true. If any code uses spl_autoload_register() that will disable any defined __autoload(). This is a good reason for why you should update PHPMailer.

Just now, SaranacLake said:

P.S. How come I didn't get an email notifying me that you responded, when "Notify me of replies" was checked?

I don't see any errors. Have you received emails from the site recently? Checked your spam folder? You're sure you have it set up to notify by email?

Share this post


Link to post
Share on other sites
8 minutes ago, requinix said:

Not broken. "Deprecated" means it works for the time being but will not in the future.

However in this case that's not entirely true. If any code uses spl_autoload_register() that will disable any defined __autoload(). This is a good reason for why you should update PHPMailer.

I don't see any errors. Have you received emails from the site recently? Checked your spam folder? You're sure you have it set up to notify by email?

Looks like I need to go figure out how to download and install the latest version of PHPMailer...  I have no recollection of how to do this, and sure hope I don't get stuck r break my code.  😞

 

I just got an email saying you replied, but I didn't get one when you replied before.  Nothing is in my spam folder, but I notice this happens sporadically.  

Share this post


Link to post
Share on other sites

P.S.  So which version of PHPMailer should I choose - assuming there are diffeent versions?

Share this post


Link to post
Share on other sites

There's only one PHPMailer. It is a name, after all. You can grab it from GitHub. Check the readme before you do anything with it, though.

Share this post


Link to post
Share on other sites

I was able to download the latest PHPMailer as a .zip since I don't know Git.

Now I'm not entirely sure what to do?!

In my current set up, I have a directory outside of my web root where I have the following PHPMailer files...

	PHPMailerAutoload.php
	class.phpmailer.php
	class.smtp.php
	

Then in my Webroot, I have a functions script which includes the PHPMailerAutoload script (and class) and then some code to create an object and send the email.

 

Not sure what to do with this new version of PHOMailer...

Share this post


Link to post
Share on other sites

I have a folder ouside my webroot called "outside_root" and I added the following...

	outside_root	    PHPMailer	        src	            Exception.php			            PHPMailer.php				            SMTP.php		

 

Then I have a function in my functions script like this...

	Function CallPHPMailer(  ){			use PHPMailer\PHPMailer\Exception;		use PHPMailer\PHPMailer\PHPMailer;	use PHPMailer\PHPMailer\SMTP;			require_once(HOME . 'outside_root/PHPMailer/src/Exception.php');				require_once(HOME . 'outside_root/PHPMailer/src/PHPMailer.php');		require_once(HOME . 'outside_root/PHPMailer/src/SMTP.php');
	// And more code to instantiate a mail object and send the email...
	}
	

 

My IDE is complaining aboout the USE statements...

 

Not sure what is wrong?

 

 

Share this post


Link to post
Share on other sites

Have you seen and read their example?

<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'user@example.com';                 // SMTP username
    $mail->Password = 'secret';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('from@example.com', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

    //Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

 

Share this post


Link to post
Share on other sites
12 hours ago, requinix said:

Have you seen and read their example?

<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
 

 

First off, how do I post code here so it doesn't get garbled up?  The code I spent a lot of time formatting last night ended up wrapping around in the [ code ] tags and probbaly made no sense?!

Since I don't know OOP and thus namespaces, I would really prefer NOT to have to install the composer thing as I feel it will just further confuse me.

In case you couldn't follow my last post, I have an fodler called "outside_webroot" that is at the same level as "public_html" and that is where I put the new PHPMailer v6 files.

Then inside my "public_html" folder, I have a "utilities" folder which contains a "functions.php" script which has the function "callPHPMailer" which is how I send emails from other scripts (e.g. "reset_passord.php")

I would *really* prefer to leave them there, and just do whatever is needed to make things work from there.

Q1.) How can I manually tweak my code so this will work without having to install Composer and further mudding things up?

Q2.) What is the "global namespace" and where is it located?

Q3.)

// These must be at the top of your script, not inside a function


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

So if I simply move those lines of code outside of the function "callPHPMailer()" and to the top of my "functions.php" script, that would fix things?

 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.