Php – Stream_socket_enable_crypto(): Peer certificate CN=`*.webhostbox.net’ did not match expected CN=`mail.maydomain.com’ using PHPMailer

emailphpphpmailer

I am using PHPMailer to send the email but I am getting an error.My domain is not SSL. If I am using smtp.gmail.com 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=`*.webhostbox.net' did not match expected CN=`mail.mydomain.com' 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';
}

PHPMailerConfig.php

<?php 
//Create a new PHPMailer instance
$mail = new PHPMailer;

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

//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');

//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

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=user@mail.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

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', 'smtp.mailgun.org'),
        '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