stmp php email – SMTP ERROR: Failed to connect to server: (0) SMTP connect() failed-ThrowExceptions

Exception or error:

I have an issue with phpMailer, I can’t send any emails, and it gives me this error:

2020-03-14 21:58:06 SMTP ERROR: Failed to connect to server: (0)
    SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
    Message could not be sent. Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

The code I’m using to send the email:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer();
$mail->SMTPDebug = SMTP::DEBUG_SERVER;                     
$mail->IsSMTP(true);
$mail->Host = 'example.com';
$mail->SMTPAuth= true;
$mail->Username='test@example.com';
$mail->Password='password';
$mail->Port = 465; 
$mail->SMTPDebug = 2; 
$mail->SMTPSecure = 'ssl';
$mail->SetFrom('test@example.com', 'Example Account');
$mail->AddAddress(`receiver@gmail.com', 'Rec Eiver');
$mail->Subject = 'Subject';
$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";
if(!$mail->Send()) {
    echo "Message could not be sent.  Error: {$mail->ErrorInfo}";
} else {
    echo 'Message has been sent';
}

I’m using GoDaddy CPanel.

How to solve:

I think that your code it’s correct.

Try to create an account mailtrap: https://mailtrap.io/signin
and use the mailtrap configs for test.

Leave a Reply

Your email address will not be published. Required fields are marked *