php - Cannot declare class PHPMailerPHPMailerException, because the name is already in use

Solution:

Try something like

if (!class_exists('PHPMailer\PHPMailer\Exception'))
{
  require '/home3/afaflawm/vendor/phpmailer/phpmailer/src/Exception.php';
  require '/home3/afaflawm/vendor/phpmailer/phpmailer/src/PHPMailer.php';
  require '/home3/afaflawm/vendor/phpmailer/phpmailer/src/SMTP.php';
}

Answer

Solution:

I kept the first two lines commented while the third one for SMTP uncommented and it started working

  //require '/home3/afaflawm/vendor/phpmailer/phpmailer/src/Exception.php';
  //require '/home3/afaflawm/vendor/phpmailer/phpmailer/src/PHPMailer.php';
  require '/home3/afaflawm/vendor/phpmailer/phpmailer/src/SMTP.php';

So the problem was caused by the first two require statements as they were already loaded by the composer on this line require 'vendor/autoload.php' except the SMTP. Therefore the error was displaying in both cases when I tried commenting all three lines once and composer the other time separately

Answer

Solution:

Based on @Far findings, the answer is:

require_once ABSPATH . 'wp- 
includes/PHPMailer/Exception.php';
require_once ABSPATH . 'wp-includes/PHPMailer/PHPMailer.php';
require_once ABSPATH . 'wp-includes/PHPMailer/SMTP.php';

Source