为什么我的在线联系表在使用我的主机中继发送后会产生警告和致命错误,而不是 Google's?

Why does my online contact form produce a Warning and Fatal Error when after sending utilising my host's relay, but not with Google's?

在 XAMPP 中使用 Google SMTP 中继和 Swiftmailer 进行测试后,我最近在线迁移了一个 Web 表单。使用我自己的收件箱的 Google 中继绝对没有问题,但是,当尝试切换到我的托管服务提供商 (Siteground) 推荐的中继和说明时,也使用基于域的电子邮件,我的表格在发送时挂起,我收到以下内容:

Warning: stream_socket_enable_crypto(): Peer certificate CN=`*.sgcpanel.com' did not match expected CN=`uk2.siteground.eu' in /home/tho/public_html/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php on line 95

Fatal error: Uncaught exception 'Swift_TransportException' with message 'Unable to connect with TLS encryption' in /home/tho/public_html/swiftmailer/lib/classes/Swift/Transport/EsmtpTransport.php:289 Stack trace: #0 /home/tho/public_html/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(118): Swift_Transport_EsmtpTransport->_doHeloCommand() #1 /home/tho/public_html/swiftmailer/lib/classes/Swift/Mailer.php(79): Swift_Transport_AbstractSmtpTransport->start() #2 /home/tho/public_html/sendmessage.php(30): Swift_Mailer->send(Object(Swift_Message)) #3 {main} thrown in /home/tho/public_html/swiftmailer/lib/classes/Swift/Transport/EsmtpTransport.php on line 289

请注意,电子邮件帐户本身运行良好。它在 Mac 邮件中设置,发送和接收完全没有问题。

我的托管服务提供商正在尝试,但我认为他们无法理解我遇到的问题。我最终将其缩小到传输功能。从下面的 PHP 中查看我的代码:

Google(按预期工作):

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587, 'tls')
        ->setUsername('foobar@googlemail.com')
        ->setPassword('APP GENERATED PASSWORD');

托管服务提供商(未按预期工作):

$transport = Swift_SmtpTransport::newInstance('uk2.siteground.eu', 25, 'tls')
    ->setUsername('email@mydomain.com')
    ->setPassword('MY EMAIL PASSWORD');

代码对我来说似乎没问题,因为它只是交换了细节。这些是Siteground提供的设置。

到目前为止我已经尝试了以下方法:

我已经提出了更多问题,他们已经为我更新了 PHP 服务器端。这现在不显示错误,而只显示数字零(即“0”)。也没有发送电子邮件,但现在这是一个单独的问题。

从错误消息看来,服务器 uk2.siteground.eu 上的 SSL 证书似乎不包含服务器的正确主机名,而是 sgcpanel.com 的通配符。 587 上的证书不包含 uk2.siteground.eu 作为服务器的有效名称。

如果您使用的是 php 5.6,他们更改了默认值以验证证书的对等点和对等点名称。

正确的解决方法是让 Siteground 向服务器添加正确的证书。您可能会在 github 上使用最新版本的 SwiftMailer 覆盖检查。

9 月添加的 setStreamOptions 可能会解决您的问题,但由于不检查它是否是正在与之通信的正确服务器,因此会稍微降低安全性。

我现在无法测试,但试试看:

$ssl_options = array(
   'ssl'         => array(
      'verify_peer'       => false,
      'verfify_peer_name' => false,
    ),
);
$transport->setStreamOptions($ssl_options);

或者数组可能是

$ssl_options = array(
   'verify_peer'       => false,
   'verfify_peer_name' => false,
);
$transport->setStreamOptions($ssl_options);

更多信息:

http://php.net/manual/en/context.ssl.php

https://github.com/swiftmailer/swiftmailer/issues/571

答案是由我的房东提供的。他们对 PHP 服务器端做了一些改动。一开始没有成功,等了一个小时左右,问题解决了。

好吧,也许不是在理想意义上排序,但至少作为一种解决方法。