Search code examples
phpphpmailermailer

PHPMailer class not found when including the class


I have installed PHPMailer from github as a download master.zip

When I use it below

<?php

require "PHPMailer.php"; //include phpmailer class

// Instantiate Class
$mail = new PHPMailer();

// Set up SMTP
$mail->IsSMTP();                // Sets up a SMTP connection
$mail->SMTPAuth = true;         // Connection with the SMTP does require authorization
$mail->SMTPSecure = "ssl";      // Connect using a TLS connection
$mail->Host = "smtp.gmail.com";  //Gmail SMTP server address
$mail->Port = 465;  //Gmail SMTP port
$mail->Encoding = '7bit';

I get the following

ubuntu@ip-172-31-35-156:/opt/PHPMailer-master/src$ php test.php
PHP Fatal error:  Uncaught Error: Class 'PHPMailer' not found in /opt/PHPMailer-master/src/test.php:6
Stack trace:
#0 {main}
  thrown in /opt/PHPMailer-master/src/test.php on line 6
ubuntu@ip-172-31-35-156:/opt/PHPMailer-master/src$ 

What am I doing wrong?

Folder structure attached enter image description here


Solution

  • PHPMailer by default is in the PHPMailer\PHPMailer namespace, you can bring it into the global namespace by using the use keyword. For example in their simple_contact_form example:

    <?php
    //Import the PHPMailer class into the global namespace
    use PHPMailer\PHPMailer\PHPMailer;