1

I have a website on wordpress 5.4 and recently it was updated to version 5.5 and now i am unable to send emails using PHPMailer. The Error i get is "There has been a critical error on your website" . The below code will work perfectly for previous wordpress Versions.

include_once( ABSPATH . WPINC . '/class-phpmailer.php' );
include_once( ABSPATH . WPINC . '/includes/PHPMailer/PHPMailerAutoload.php' );

$mailserver_url   = "[mailserver_url]";
$mailserver_login =  "[mailserver_login]";
$mailserver_pass  =  '[mailserver_pass]';
$mailserver_port  =  '[mailserver_port]';
$email = '[email]';

$mail             = new PHPMailer;
$mail->ClearAttachments();
$mail->isSMTP();
$mail->SMTPAuth    = true;
$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer'       => false,
        'verify_peer_name'  => false,
        'allow_self_signed' => false
    )
);
$mail->Host        = $mailserver_url;
$mail->Port        = $mailserver_port;
$mail->Username    = $mailserver_login;
$mail->Password    = $mailserver_pass;
$mail->setFrom( $email );
$mail->addReplyTo( $mailserver_login );
$mail->addAddress( $email );
$mail->Subject = 'The Subject';
$mail->isHTML();
$mail->Body = '<p>Helloo</p>';
if ( $mail->Send() ) {
   echo 'sent';
}

I looked into the below article but i wasnt able to fix it https://wordpress.org/support/topic/fatal-error-after-updating-to-wp-5-5/

Krishneel Singh
  • 324
  • 1
  • 8
  • 16
  • Check the server's error log, or activate WP_DEBUG and error logging into file, this should give you more details on the critical error. Also, please add WHEN exactly the critical error is thrown: on page load, on email sending? – jasie Aug 28 '20 at 10:08

4 Answers4

4

If you want to use it in a plugin and keep PHP Mailer compatible for older WordPress version, this is the complete solution.

global $wp_version;
if( $wp_version < '5.5') {
    require_once(ABSPATH . WPINC . '/class-phpmailer.php');
    require_once(ABSPATH . WPINC . '/class-smtp.php');
    $mail = new PHPMailer( true );
}
else {
    require_once(ABSPATH . WPINC . '/PHPMailer/PHPMailer.php');
    require_once(ABSPATH . WPINC . '/PHPMailer/SMTP.php');
    require_once(ABSPATH . WPINC . '/PHPMailer/Exception.php');
    $mail = new PHPMailer\PHPMailer\PHPMailer( true );
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Patrick Buntsma
  • 621
  • 5
  • 7
2

It’s because you are attempting to load an old version of PHPMailer that no longer exists in WordPress, and getting a fatal error as a result. You should not have to load PHPMailer yourself because WordPress supplies it as standard, so refer to their docs for how to send messages, and how to create a hook to inject a custom configuration.

You can still load it yourself, but by doing so you bypass all that WP is doing for you and become liable for everything that goes with it, including loading the classes properly. To help update your code to work with PHPMailer 6.x, read the upgrade guide, or this question.

Synchro
  • 35,538
  • 15
  • 81
  • 104
  • The stack overflow question will be very helpful to WordPress developers as i used that to fix my issue.https://stackoverflow.com/questions/45940509/how-should-i-upgrade-from-phpmailer-5-2-to-6-0 – Krishneel Singh Sep 10 '20 at 21:38
2

With WordPress upgrade to version 5.5 it may be necessary to include the PHPMailer SMPT.php file.

include_once (ABSPATH . WPINC . '/class-phpmailer.php');
include_once (ABSPATH . WPINC . '/PHPMailer/SMTP.php');
$mail = new PHPMailer ();
2

WordPress has now moved PHP mailer into a subdirecotry, you need to update your code as follows:

At the top of your function add:

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

The update your code further down to:

require_once(\ABSPATH . \WPINC . "/PHPMailer/PHPMailer.php");
require_once(\ABSPATH . \WPINC . "/PHPMailer/Exception.php");       
require_once(\ABSPATH . \WPINC . "/PHPMailer/SMTP.php");
$mail = new PHPMailer();