Home  >  Q&A  >  body text

Encountered PHPMailer undefined type error (tried using Composer also didn't work)

I don't know why I'm getting this "PHPMailer undefined type" error. First, I tried downloading the zip directly and copying the code from github, the same problem appeared, then I downloaded composer which I didn't want to download, but after downloading it still showed the same error.

Can I have any thoughts on this? (Screenshot below)

Composer img error

Same error occurs without composer img

Catalog Pictures

This is the code

session_start();

// Import PHPMailer classes into the global namespace 
use PHPMailerPHPMailerPHPMailer; 
use PHPMailerPHPMailerException; 
 
require 'PHPMailer/src/Exception.php'; 
require 'PHPMailer/src/PHPMailer.php'; 
require 'PHPMailer/src/SMTP.php'; 
 
$mail = new PHPMailer(true);   // error coming here
 
$mail->isSMTP();                      // Set mailer to use SMTP 
$mail->Host = 'smtp.gmail.com';       // Specify main and backup SMTP servers 
$mail->SMTPAuth = true;               // Enable SMTP authentication 
$mail->Username = 'user@gmail.com';   // SMTP username 
$mail->Password = 'gmail_password';   // SMTP password 
$mail->SMTPSecure = 'tls';            // Enable TLS encryption, `ssl` also accepted 
$mail->Port = 587;                    // TCP port to connect to 
 
// Sender info

Thanks

P粉921165181P粉921165181316 days ago597

reply all(1)I'll reply

  • P粉734486718

    P粉7344867182023-11-13 16:24:48

    When you use composer, just autoload (it does all the "need" work)

    include_once 'vendor/autoload.php'
    use PHPMailer\PHPMailer\PHPMailer; 
    use PHPMailer\PHPMailer\Exception;
    
    // require 'PHPMailer/src/Exception.php'; < -- this is no longer used

    reply
    0
  • Cancelreply