Php – Stream_socket_enable_crypto(): Peer certificate CN=`*’ did not match expected CN=`’ using PHPMailer


I am using PHPMailer to send the email but I am getting an error.My domain is not SSL. If I am using with my Gmail id then emails are going to inbox but when I am using my hosting details then I am getting an error

 Warning: stream_socket_enable_crypto(): Peer certificate CN=`*' did not match expected CN=`' in C:\xampp\htdocs\sendmail\mail\class.smtp.php on line 337
 Mailer Error: SMTP connect() failed.

If I set $mail->SMTPSecure = 'tls'; to $mail->SMTPSecure = 'false'; then not getting an error but emails are going to spam.
Even I tried below code.

$mail->SMTPOptions = array (
        'ssl' => array(
            'verify_peer'  => false,
            'verify_peer_name'  => false,
            'allow_self_signed' => true));

Would you help me out in this?

Thank you

require 'mail/PHPMailerAutoload.php';
function sendMail($subject, $content, $email){
    $phpMailerSubject = $subject;
    $phpMailerText = $content;
    $phpMailerTo = $email; 
    include 'mail/PHPMailerConfig.php';


//Create a new PHPMailer instance
$mail = new PHPMailer;

$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = '';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "";
$mail->Password = "Pass#@123";
$mail->setFrom('', 'naren');
$mail->addReplyTo('', 'naren');
$mail->addAddress($phpMailerTo, 'Customer');
$mail->Subject = $phpMailerSubject;
$mail->AltBody = ' ';

//Attach an image file

//send the message, check for errors
if (!$mail->send()) { echo "Mailer Error: " . $mail->ErrorInfo;
} else { 
echo "sucessfully"; 

Best Solution

I will post my answer since I spend a lot of time trying to fix this issue.

I have a Laravel web application hosted in GoDaddy

So, I add my email setup to the .env and left the MAIL_ENCRYPTION empty


also, in config/mail.php, I left the encryption as the next one, where MAIL_ENCRYPTION is empty again

'mailers' => [
    'smtp' => [
        'transport' => 'smtp',
        'host' => env('MAIL_HOST', ''),
        'port' => env('MAIL_PORT', 587),
        'encryption' => env('MAIL_ENCRYPTION', ''),
        'username' => env('MAIL_USERNAME'),
        'password' => env('MAIL_PASSWORD'),
        'timeout' => null,

after save changes, I just run php artisan config:cache and then php artisan config:clear and voila, it is working now

Hope to be usefull for somebody

Related Question