-1

I'm getting the error : Class 'PHPMailer' not found in PHPMailer

I know the answer is probably out there but for my code, I can't seem to work it out.

I have a .php file where I created a class called Email, then a function inside this called sendEmail. Inside this function there is the PHPMailer files and code to send an email.

My code is :

function sendMail($con, $siteName, $siteLogo, $email1, $email2, $email3, $email4, $email5, $title, $message, $buttonText, $buttonLink) {
    # Get the variables of the email using the variables inside the function's brackets upon request

    error_reporting(E_ALL);

    global $gmailEmail;
    global $gmailPassword;
    global $siteUrl;

    require $siteUrl.'PHPMailer/PHPMailer.php';
    require $siteUrl.'PHPMailer/SMTP.php';
    require $siteUrl.'PHPMailer/Exception.php';

    $mail = new PHPMailer;

    $body = $this->emailTemplate($con, $siteName, $siteLogo, $email1, $email2, $email3, $email4, $email5, $title, $message, $buttonText, $buttonLink);

    $mail->Host      = "smtp.gmail.com";
    $mail->SMTPDebug = 1;
    $mail->SMTPAuth  = true;
    $mail->Host      = "smtp.gmail.com";
    $mail->Port      = 587;
    $mail->Username  = $gmailEmail;
    $mail->Password  = $gmailPassword;

    $mail->SetFrom("admin@".$siteName, $sitename." Admin");
    $mail->AddReplyTo("admin@".$siteName, $sitename." Admin");
    $mail->Subject = $title;
    $mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
    $mail->MsgHTML($body);

    $emails = array($email1, $email2, $email3, $email4, $email5);

    # Foreach email that is not empty, send the email
    foreach((array)$emails as $email) {
        $mail->AddAddress($email);
        if (!$mail->Send()) {
            echo "Mailer Error: " . $mail->ErrorInfo;
        } else {
            echo "Message sent!";
        }
    }
}

But the error lies on the line of the code: $mail = new PHPMailer; , saying Class 'PHPMailer' not found

Someone said it's the use's :

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

But whenever I include these, I get this error: Class 'PHPMailer\PHPMailer\PHPMailer' not found

Ben Za
  • 21
  • 1
  • 9
  • PHPMailer requires Composer. Then you would just `require_once("vendor/autoload.php")` – IsThisJavascript Dec 24 '17 at 21:05
  • Thats not including a file. Thats importing/using a class in a namespace. You will need to include the actual file using autoloader or `include()`, `include_once()`, `require()` or `require_once()` – frz3993 Dec 24 '17 at 21:08
  • @frz3993 I have done according to their documentation. Look in my function, I have included require() the files. – Ben Za Dec 24 '17 at 21:10
  • What is site `$siteUrl`? If its an actual url, the allow_url_include must be turned on. This is not recommended. Try using absolute path to the file. – frz3993 Dec 24 '17 at 21:12
  • @frz3993 Site url is my default url for the site. It's not the error as I just replaced it with the url and it didn't work still. – Ben Za Dec 24 '17 at 21:14
  • @AniketSahrawat No it's not. – Ben Za Dec 24 '17 at 21:15
  • @AniketSahrawat there is no other way I can do it. Anyway, don't go off topic. – Ben Za Dec 24 '17 at 21:18
  • Ok, try moving the require outside the function, the `use` come after the require – frz3993 Dec 24 '17 at 21:20
  • Where is the actual structure information? How you connect PHPMail class, via composer or the other way (if it's, then how)? Also don't provide only your function, there may be problems with namespaces. – Abraham Tugalov Dec 24 '17 at 21:24

1 Answers1

3

You don't have to have composer (though it's a good idea), but if you don't, you do have to load the classes yourself, and you have to import them into your namespace with use statements. All of this is covered in the readme and examples provided with PHPMailer, so it's not exactly kept secret.

The use statements must appear at the top of your script, before you try to use the classes, so perhaps the problem is that you're putting them within your function, which won't work – all that is standard PHP syntax, nothing unusual.

Alternatively you can use fully qualified class names, like this:

$mail = new PHPMailer\PHPMailer\PHPMailer;

Also I can see that you've based your code on a very old, obsolete example. That'a a bad idea - always use the latest.

Synchro
  • 35,538
  • 15
  • 81
  • 104