Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot declare class PHPMailer\PHPMailer\Exception, because the name is already in use

Tags:

php

phpmailer

The error: Cannot declare class PHPMailer\PHPMailer\Exception, because the name is already in use.

Below is the code being used

 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';
// Load Composer's autoloader
 require 'vendor/autoload.php';
   use PHPMailer\PHPMailer\PHPMailer;
 use PHPMailer\PHPMailer\SMTP;
 use PHPMailer\PHPMailer\Exception;
   function SendSMTPemail(){
  try {
 $mail = new PHPMailer(true);
  $mail->SMTPDebug = SMTP::DEBUG_SERVER;  
    $mail->Host='ssl';
       $mail->isSMTP();                                            
    $mail->SMTPAuth   = true;                                   
     $mail->Username='[email protected]'; 
    $mail->Password='xxxxxxx'; 
       $mail->setFrom('[email protected]');
       $mail->AddAddress('[email protected]');                             // SMTP password
    $mail->SMTPSecure = 'ssl';
    $mail->Port       = 465;                                    // TCP port to connect to
    $mail->addReplyTo('[email protected]', 'Information');
        $mail->isHTML(true);  
   $mail->Subject = "Appointment Details";
    $mail->Body    = "Dear, <br> Please be informed that you have an appointment  tommorow   <br>      Regards <br> ";
    $mail->AltBody = '';
   if(!$mail->Send()) {
           echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
         echo "Message sent!";
    }
     //echo "success";
        // $globals['emailstatus'] =true;
        // header("Location: sendEmail.php");
   } catch (Exception $e) {
       //echo "fail";
           // $globals['emailstatus'] =false;
    echo "Message could not be sent.";
   }
     }
   SendSMTPemail();

If following lines of code are commented,

   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';

Then it shows the following error:

   Uncaught Error: Class 'PHPMailer\PHPMailer\SMTP' not found 

If following lines of code are commented,

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

Then it shows the following error:

     Cannot declare class PHPMailer\PHPMailer\Exception, because the name is already in use 

Following is the directory and its placement: enter image description here

I had once used PHPMailer previosuly using Gmail's SMTP which was working fine. Can someone help as to why this error is being displayed though the required files are in the right directory?

Findings (Edits) I realized there is a PHPMailer folder residing in also wp_content folder as well as wp_includes. I had uploaded PHPMailer folder to wp_content long time ago when I was working with PHPmailer for gmail. I have multiple copies of this folder in the files. One in the root directory in vendor folder which was created by composer and others in wp_content and wp_includes. wp_includes is strange as I dont know if the composer created or what.. What should I do. I would like to just use the one in wp_content as it was working fine when I had used it using gmail SMTP @Synchro

like image 846
Far Avatar asked Jan 25 '26 23:01

Far


1 Answers

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';
}
like image 142
PJunior Avatar answered Jan 28 '26 16:01

PJunior



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!