PHP - Use PHPMailer in Custom Class

Viewed 285

I'm trying to use PHPMail in a class but I get an error at the line indicated below.

Can anybody see why?

    class SendMail {

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

    public function __construct()
    {
        $this->debug();
        $this->includes();
    }

    public function send_mail()
    {
        $mail = new PHPMailer(true); // ERROR
    }

    private function includes()
    {
        require __DIR__ . '/../config.php';
        require '/var/composer/phpmailer/vendor/autoload.php';
    }

    private function debug()
    {
        ini_set('display_errors', 0); // 0 = Only Warnings, 1 = All Notifications
        ini_set('display_startup_errors', 1);
        error_reporting(E_ALL);
    }
    }

I've tried moving the "new" declaration into the constructor but that didn't make any difference.

I get a 500 error and the VSCode debugger shows:

enter image description here

3 Answers

You are misusing the Traits .

I believe that PHPMailer is not a traits.

you need to use it as a namespace not as a trait.

<?php
namespace \XYZ;

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

class SendMail {
    // ....
}

note that you are need to use autoloader.

Final Working Code:

<?php

use PHPMailer\PHPMailer\PHPMailer;

class SendMail
{
    public function __construct()
    {
        $this->debug();
        $this->includes();
    }

    public function send_mail()
    {
        $mail = new PHPMailer(true);
        echo 'Hello World';
    }

    private function includes()
    {
        require __DIR__ . '/../config.php';
        require '/var/composer/phpmailer/vendor/autoload.php';
    }

    private function debug()
    {
        ini_set('display_errors', 0); // 0 = Only Warnings, 1 = All Notifications
        ini_set('display_startup_errors', 1);
        error_reporting(E_ALL);
    }
}

You can skip to load the autoloader this way (if this is the only module it will probably be faster):

<?php

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

$yourmail = new SendMail;
$yourmail->send("info@destination.com", "Destination Name", "Mail Subject", "<p>HTML Content</p>");

class SendMail {
    public function __construct() {
        $this->includes();
    }

    public function send($email, $name, $subject, $html_content) {
        
        $mail = new PHPMailer();
        $mail->IsSMTP();
        $mail->SMTPDebug  = 0;  
        $mail->SMTPAuth   = TRUE;
        $mail->SMTPSecure = "tls";
        $mail->Port       = SMTP_PORT;     # to be defined
        $mail->Host       = SMTP_SERVER;   # to be defined
        $mail->Username   = SMTP_USER;     # to be defined
        $mail->Password   = SMTP_PASSWORD; # to be defined

        $mail->IsHTML(true);
        $mail->AddAddress($email, $name);
        $mail->SetFrom(SMTP_USER, SMTP_NAME);
        $mail->AddReplyTo(SMTP_USER, SMTP_NAME);
        #$mail->AddCC("cc-recipient-email", "cc-recipient-name");
        $mail->Subject = $subject;

        $mail->MsgHTML($html_content);

        if(!$mail->Send()) {
            # you are having some trouble
            return false;
        } else {
            return true;
        }
    }

    private function includes()
    {
        require_once 'vendor/phpmailer/phpmailer/src/Exception.php';
        require_once 'vendor/phpmailer/phpmailer/src/PHPMailer.php';
        require_once 'vendor/phpmailer/phpmailer/src/SMTP.php';
    }

}

Related