Search code examples
phpphpmailer

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


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='xy@xy.com'; 
    $mail->Password='xxxxxxx'; 
       $mail->setFrom('xy@xy.com');
       $mail->AddAddress('yz@yz.com');                             // SMTP password
    $mail->SMTPSecure = 'ssl';
    $mail->Port       = 465;                                    // TCP port to connect to
    $mail->addReplyTo('ab@ab.com', '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


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