On my page I have a contact form which uses PHP Mailer. At the beginning, for testing purposes only, I used it with my Gmail account. Everything worked like a charm. Then I decided to change the e-mail service from Gmail to one which comes from my hosting provider. Then the problems have started. Every time I attempt to send an email, an exception occurs:
2020-05-13 20:53:44 CLIENT -> SERVER: STARTTLS
2020-05-13 20:53:44 SERVER -> CLIENT: 220 ready for tls
SMTP Error: Could not connect to SMTP host.
2020-05-13 20:53:44 CLIENT -> SERVER: QUIT
2020-05-13 20:53:44 SERVER -> CLIENT: 454 TLS connection failed: error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure (#4.3.0)
2020-05-13 20:53:44 SMTP ERROR: QUIT command failed: 454 TLS connection failed: error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure (#4.3.0)
This is my code for sending an e-mail:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
require 'PHPMailer.php';
require 'SMTP.php';
require 'Exception.php';
class Mailer
{
private $mail;
public function __construct($host, $user, $password, $port = 587)
{
$this->mail = new PHPMailer();
try
{
$this->mail->SMTPDebug = SMTP::DEBUG_SERVER;
$this->mail->CharSet = "UTF-8";
$this->mail->isSMTP();
$this->mail->Host = $host;
$this->mail->SMTPAuth = true;
$this->mail->SMTPSecure = 'ssl';
$this->mail->Username = $user;
$this->mail->Password = $password;
$this->mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$this->mail->Port = $port;
}
catch(Exception $e)
{
echo "Exception: " . $e;
}
}
public function Send($from, $alias, $to, $subject, $body, $cc)
{
try
{
$this->mail->setFrom($from, $alias);
$this->mail->addAddress($to);
$this->mail->isHTML(true);
$this->mail->Subject = $subject;
$this->mail->Body = $body;
if($cc !== '')
{
$this->mail->AddCC($cc);
}
if(!$this->mail->send())
{
die($this->mail->ErrorInfo);
}
return true;
}
catch (Exception $e)
{
return false;
}
}
I suppose I configure PHP Mailer wrongly when it comes to TLS/SSL, as I am pretty newbie to this. It may be an expedient information that my webpage uses TLS 1.3 encryption
Here's a tip, if it works, don't forget a vote to strengthen. Let's go to the settings ...
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => false
)
);
Remember that this is a palliative solution to work immediately, but I recommend investigating the problem. Another detail do tests using ports 465 and 587 sometimes it can be that!
$mail->Port = "587";
$mail->SMTPSecure = "tls";
Another thing I didn't understand ... why would I be using $ this->mail->SMTPSecure twice?
$this->mail->SMTPSecure = 'ssl';
$this->mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;