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:
CodePudding user response:
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.
CodePudding user response:
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);
}
}