I use the following function for sending e-mail using PHPMailer:
public function test_emailAction()
{
require_once (APP_PATH.'/lib/PHPMailer/src/PHPMailer.php');
require_once (APP_PATH.'/lib/PHPMailer/src/Exception.php');
require_once (APP_PATH.'/lib/PHPMailer/src/SMTP.php');
$account= "email@gmail.com";
$password= "**********";
$from_email= "from_email@gmail.com";
$from_name= "from_name";
$to_email= "to_email@gmail.com";
$to_name= "to_name";
$subject="Test subject";
$msg="Test message";
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->CharSet = 'UTF-8';
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth= true;
$mail->Port = 465;
$mail->Username= $account;
$mail->Password= $password;
$mail->SMTPSecure = 'ssl';
//Recipients
$mail->setFrom($from_email, $from_name);
$mail->addAddress($to_email, $to_name);
//Content
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $msg;
if(!$mail->send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "E-Mail has been sent";
}
}
The function is within a class. Above/outside of the class I have:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
but I get the following error message:
Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP Error: Could not authenticate. in
When I use Port 587 instead of Port 465 the error message is:
Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP connect() failed.
What am I doing wrong? Thank you in advance