1

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 = "[email protected]";
$mail->Password = "Pass#@123";
$mail->setFrom('[email protected]', 'naren');
$mail->addReplyTo('[email protected]', '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"; 
}
0

4 Answers 4

3

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
[email protected]
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

1
  • Thanks for the answer. I asked 2 years back to this question. After that every thing is change :) Commented Apr 15, 2020 at 18:20
2

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.

2
  • There was an issue with my ISP provider. I Commented Jul 31, 2017 at 5:53
  • If I used smtp.gmail.com then emails were going to inbox. I was getting the issue on my own server. and now my issue got resolved. Commented Jul 31, 2017 at 6:02
0

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.