skip to Main Content

I am trying to use PHPMailer to send email, and I’m writing the PHP in cPanel through Bluehost. Below is my code so far. The file is in the PHPMailermaster folder, along with the src folder.

The code fails upon reaching the “new PHPMailer” line (so when I run it, the page would echo “Reached checkpoint 1” but not “Reached checkpoint 2”).

I have never used PHP before so it is very possible that I am doing the PHP wrong, and it seems like almost all tutorials about this are for older versions of PHPMailer, so all I really have to go off of is their GitHub page. My code is mostly copied from there, with a little modification for the use paths. I also commented out the autoloader because I don’t have that and I’m not sure what it does.

<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use srcPHPMailer;
use srcSMTP;
use srcException;

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

// Load Composer's autoloader
// require 'vendor/autoload.php';

echo "Reached checkpoint 1";

// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);

echo "Reached checkpoint 2";

try {
    //Server settings
    $mail->SMTPDebug = 1;                      // Enable verbose debug output
    $mail->isSMTP();                                            // Send using SMTP
    $mail->Host       = 'smtp.gmail.com';                    // Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = '<my username>';                     // SMTP username
    $mail->Password   = '<my password>';                               // SMTP password
    $mail->SMTPSecure = 'ssl';         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
    $mail->Port       = 465;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('<my email>@gmail.com');
    $mail->addAddress('<test email>@gmail.com');     // Add a recipient
    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

The error thrown is the following (adminusername is just a placeholder for my cpanel username):

Fatal error: Uncaught Error: Class 'srcPHPMailer' not found in /home1/<adminusername>/public_html/PHPMailermaster/mail.php:21 Stack trace: #0 {main} thrown in /home1/<adminusername>/public_html/PHPMailermaster/mail.php on line 21

2

Answers


  1. Chosen as BEST ANSWER

    I believe I have solved the issue. To use PHPMailer without Composer, I created an include_directory, put the PHPMailermaster folder in there, and added it to php.ini using this method. I imported the necessary class files like so:

    use PHPMailerPHPMailerPHPMailer;
    use PHPMailerPHPMailerException;
    use PHPMailerPHPMailerSMTP;
    require '../include_directory/PHPMailermaster/src/Exception.php';
    require '../include_directory/PHPMailermaster/src/PHPMailer.php';
    require '../include_directory/PHPMailermaster/src/SMTP.php';
    

    And it worked. I think the problem was how I was importing and organizing the files.


  2. Check the ‘namespace’ clausule in “src/PHPMailer.php” file. It’s probable that the PHPMailer class is not in the src namespace.

    You should use now composer for installing it, after that, uncomment the require 'vendor/autoload.php'; line, and use something like:

    use PHPMailerPHPMailerPHPMailer;
    
    require 'vendor/autoload.php';
    
    $mail = new PHPMailer(true);
    
    

    More info in https://github.com/PHPMailer/PHPMailer

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search