Class PHPMailer not found


#1

I started with the blog post here, because I need to send attachments:

I’ve also followed the instructions and created the sample shown in the first answer at https://stackoverflow.com/questions/44496171/phpmailer-gives-http-error-500-when-i-try-to-test-email-on-cloudways and have already run composer: composer require phpmailer/phpmailer in the directory.
The key lines of the file being:

<?php require_once "vendor/autoload.php"; //PHPMailer Object $mail = new PHPMailer; //From email address and name ...

I still get the following error each time:

PHP Fatal error: Uncaught Error: Class ‘PHPMailer’ not found in /home/xxx.cloudwaysapps.com/xxx/public_html/iframe/mailtest.php:3
Stack trace:
#0 {main}
thrown in /home/xxx.cloudwaysapps.com/xxx/public_html/iframe/mailtest.php on line 3

Something simple I’m missing?

I’ve boiled it down to the following tests:

This works:

<?php $to = "xxx@cloudways.com"; $subject = "Testing"; $txt = "Hello world!"; mail($to,$subject,$txt,'From: xxx@cloudways.com ','-f mujtaba.naeem@cloudways.com '); ?>

This never gets past line 3:

<?php require_once "vendor/autoload.php"; //PHPMailer Object $mail = new PHPMailer; //From email address and name $mail->From = "xxx@example.com"; $mail->FromName = "Full Name"; //To address and name $mail->addAddress("xxx@example.com", "Recepient Name");//Recipient name is optional $mail->isHTML(true); $mail->Subject = "Subject Text"; $mail->Body = "Mail body in HTML"; $mail->AltBody = "This is the plain text version of the email content"; //Enable SMTP debugging. $mail->SMTPDebug = 3; //Set PHPMailer to use SMTP. $mail->isSMTP(); //Set SMTP host name ... etc with credentials

Again, I’ve run composer in this directory. Output is:

iframe$ composer require phpmailer/phpmailer
Using version ^6.0 for phpmailer/phpmailer
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files

Thanks for your help.


Phpmailer not sending emails
#2

Hi Eballer,
I hope you are doing good.

PHPmailer has the latest version now which is 6.x. This version is not using autoload.php file.

After installing PHPmailer library to your project you need to add exact path of the files in your code.
The files are installed in vendor folder similar like other packages.

Below is the example:

<?php

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

require 'vendor/phpmailer/phpmailer/src/Exception.php';
require 'vendor/phpmailer/phpmailer/src/PHPMailer.php';
require 'vendor/phpmailer/phpmailer/src/SMTP.php';

$mail = new PHPMailer();

Rest code is same you can try now to send email. Let me know if any problem arise again.

Regards


#3

That was it, thank you! Any way to get in touch with cloudways developers to have them update the blog entry that their support team points to?


#4

Hi, Glad to know this works now. You can ask me about any feedback. I’ll take care of the blog update don’t worry.


#5

It would be nice to have that blog post updated. Spent almost a day trying to figure this out until I found this post.

Thank you