简体   繁体   中英

Stream_socket_enable_crypto(): Peer certificate CN=`*.webhostbox.net' did not match expected CN=`mail.maydomain.com' 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 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"; 
}

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

Read the PHPMailer docs that the error points you at; it explains how to diagnose this problem.

You're asking to connect to smtp.gmail.com , but you're actually connecting to mail.webhostbox.net (I gather, from what you posted). Not surprisingly, the TLS certificate for that doesn't match gmail's domain name, and so it fails. You try to force it to work by disabling verification, which won't work as you're now forging the from address, causing SPF failures, and thus will end up either rejected or in spam, as you're seeing.

You need to either change your from address to match the SPF your ISP provides, get them to let you use SMTP properly, or switch to a more enlightened ISP.

Posting this up for reference to be found easily.

A lot of people upgrading to PHP 5.6+ are running into the following error:

ErrorException: Email to [email address] failed: stream_socket_enable_crypto(): Peer certificate CN= [hostname]' did not match expected CN= [target hostname]' - library/Zend/Mail/Protocol/Smtp.php:206

As of PHP 5.6 peer verification is enabled by default ( http://php.net/manual/en/migration56.openssl.php ).

If you are running WHM or Plesk, I found the issue could be resolved as follows:

WHM: Change the “Restrict outgoing SMTP to root, exim, and mailman (FKA SMTP Tweak)” from “On” to “Off”.

Plesk: Create a new subscription with the URL set as the server host address, then assign it a SSL cert via the lets encrypt plugin.

Every other information looks ok. only change your encryption type from tls to ssl

Instead of: $mail->SMTPSecure = 'tls';

Do $mail->SMTPSecure = 'ssl';

More info about mail encryption:

Email encryption involves encrypting, or disguising, the content of email messages in order to protect potentially sensitive information from being read by anyone other than intended recipients. Email encryption often includes authentication.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM