【问题标题】:Stream_socket_enable_crypto(): Peer certificate CN=`*.webhostbox.net' did not match expected CN=`mail.maydomain.com' using PHPMailerStream_socket_enable_crypto():对等证书 CN=`*.webhostbox.net' 与使用 PHPMailer 的预期 CN=`mail.maydomain.com' 不匹配
【发布时间】:2018-01-05 04:19:40
【问题描述】:

我正在使用 PHPMailer 发送电子邮件,但收到错误消息。我的域不是 SSL。如果我将 smtp.gmail.com 与我的 Gmail id 一起使用,那么电子邮件将发送到收件箱,但是当我使用我的托管详细信息时,我会收到一个错误

 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.

如果我将$mail->SMTPSecure = 'tls'; 设置为 $mail->SMTPSecure = 'false';然后没有收到错误,但电子邮件会变成垃圾邮件。 即使我尝试了下面的代码。

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

你能帮我解决这个问题吗?

谢谢

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"; 
}

【问题讨论】:

  • 有人可以帮我吗?

标签: php email phpmailer


【解决方案1】:

Read the PHPMailer docs 错误指向你;它解释了如何诊断这个问题。

您要求连接到smtp.gmail.com,但您实际上是连接到mail.webhostbox.net(我从您发布的内容中收集到)。毫不奇怪,它的 TLS 证书与 gmail 的域名不匹配,因此它失败了。您尝试通过禁用验证来强制它工作,这不起作用,因为您现在正在伪造发件人地址,导致 SPF 失败,因此最终会被拒绝或垃圾邮件中,正如您所看到的。

您需要更改您的发件人地址以匹配您的 ISP 提供的 SPF,让他们让您正确使用 SMTP,或者切换到更开明的 ISP。

【讨论】:

  • 我的 ISP 提供商有问题。我
  • 如果我使用 smtp.gmail.com,那么电子邮件将进入收件箱。我在自己的服务器上遇到了问题。现在我的问题得到了解决。
【解决方案2】:

我会发布我的答案,因为我花了很多时间试图解决这个问题。

我有一个托管在 GoDaddy 中的 Laravel Web 应用程序

所以,我将我的电子邮件设置添加到 .env 并将 MAIL_ENCRYPTION 留空

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}"

另外,在 config/mail.php 中,我将加密保留为下一个,其中 MAIL_ENCRYPTION 再次为空

'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,
    ],

保存更改后,我只运行 php artisan config:cache 然后 php artisan config:clear 瞧,它现在可以工作了

希望对某人有用

【讨论】:

  • 感谢您的回答。我问了2年前这个问题。在那之后,一切都变了:)
【解决方案3】:

将其发布以供参考,以便轻松找到。

很多人升级到 PHP 5.6+ 时遇到以下错误:

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

从 PHP 5.6 开始,默认启用对等验证 (http://php.net/manual/en/migration56.openssl.php)。

如果您正在运行 WHM 或 Plesk,我发现问题可以通过以下方式解决:

WHM:将“将传出 SMTP 限制为 root、exim 和 mailman (FKA SMTP Tweak)”从“开”更改为“关”。

Plesk:使用设置为服务器主机地址的 URL 创建一个新订阅,然后通过 Let encrypt 插件为其分配 SSL 证书。

【讨论】:

    【解决方案4】:

    所有其他信息看起来都不错。只需将您的加密类型从 tls 更改为 ssl

    而不是:$mail-&gt;SMTPSecure = 'tls';

    $mail-&gt;SMTPSecure = 'ssl';

    More info about mail encryption:

    电子邮件加密涉及加密或伪装电子邮件的内容,以保护潜在的敏感信息不被预期收件人以外的任何人阅读。电子邮件加密通常包括身份验证。

    【讨论】:

      猜你喜欢
      • 2017-05-24
      • 2019-05-27
      • 1970-01-01
      • 2016-01-07
      • 2019-12-17
      • 2016-12-07
      • 1970-01-01
      • 1970-01-01
      • 2012-11-06
      相关资源
      最近更新 更多