PHPMailer 不会使用 SSL 发送,但 Outlook 会

标签 php email ssl phpmailer

我在尝试使用 PHPMailer 和 SSL 在我的本地计算机上发送电子邮件时遇到问题(在没有 SSL 的情况下工作正常),我不知道从哪里开始查找错误的来源。

错误:

2015-02-08 01:13:29 Connection: opening to ssl://mail.mydomain.com:465, t=300, opt=array ( ) 2015-02-08 01:13:29 SMTP ERROR: Failed to connect to server: (0) 2015-02-08 01:13:29 SMTP connect() failed. Message could not be sent.Mailer Error: SMTP connect() failed.

这是我用来发送的代码:

$mail->SMTPDebug = 4;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'mail.mydomain.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'admin@mydomain.com';                 // SMTP username
$mail->Password = 'xxxx';                           // SMTP password
$mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;                                    // TCP port to connect to

$mail->From = 'admin@mydomain.com';
$mail->FromName = 'Mailer';
$mail->addAddress('personalemail@gmail.com');     // Add a recipient

这是我尝试/观察到的:

  • 相同的代码在我的服务器(也存储电子邮件服务器)上运行良好
  • 没有 SSL,我可以从本地主机或服务器发送电子邮件。
  • 我可以使用带 SSL 的 gmail 帐户从我的家用计算机和 PHPMailer 发送邮件。
  • 使用与上述代码相同的设置,我可以在家中使用 Microsoft Outlook 通过 SSL 发送/接收消息(询问我是否要接受证书)。
  • 我已经尝试刷新 DNS(使用 ipconfig/flushdns),但它什么也没做。

我真的不知道从哪里开始寻找,因为我可以使用 gmail 帐户通过 ssl 发送邮件,但不能使用我自己的帐户,但是如果我使用 outlook 就可以。

有什么我搞砸的想法吗?

编辑:

nslookup 结果:

C:\Users\xxx>nslookup mail.xxxx.com
Server:  UnKnown
Address:  192.168.1.1

Non-authoritative answer:
Name:    xxxx.com
Address:  110.232.yyy.zzz (this is correct)
Aliases:  mail.xxxx.com

$ openssl s_client -starttls smtp -crlf -connect mail.xxxx.com:465
CONNECTED(00000003)

$ openssl s_client -crlf -connect mail.xxxx.com:465
CONNECTED(00000003)
depth=1 C = US, ST = Illinois, L = Chicago, O = "Trustwave Holdings, Inc.", CN = "Trustwave Organization Validation CA, Level 2", emailAddress = ca@trustwave.com
verify error:num=20:unable to get local issuer certificate
verify return:0
---
Certificate chain
 0 s:/CN=*.zuver.net.au/O=Zuver Pty Ltd/L=Narre Warren/ST=VIC/C=AU
   i:/C=US/ST=Illinois/L=Chicago/O=Trustwave Holdings, Inc./CN=Trustwave Organization Validation CA, Level 2/emailAddress=ca@trustwave.com
 1 s:/C=US/ST=Illinois/L=Chicago/O=Trustwave Holdings, Inc./CN=Trustwave Organization Validation CA, Level 2/emailAddress=ca@trustwave.com
   i:/C=US/O=SecureTrust Corporation/CN=SecureTrust CA
---
Server certificate
-----BEGIN CERTIFICATE-----
MIIFLTCCBBWgAwIB...
-----END CERTIFICATE-----
subject=/CN=*.zuver.net.au/O=Zuver Pty Ltd/L=Narre Warren/ST=VIC/C=AU
issuer=/C=US/ST=Illinois/L=Chicago/O=Trustwave Holdings, Inc./CN=Trustwave Organization Validation CA, Level 2/emailAddress=ca@trustwave.com
---
No client certificate CA names sent
---
SSL handshake has read 3822 bytes and written 624 bytes
---
New, TLSv1/SSLv3, Cipher is DHE-RSA-AES256-GCM-SHA384
Server public key is 2048 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
SSL-Session:
    Protocol  : TLSv1.2
    Cipher    : DHE-RSA-AES256-GCM-SHA384
    Session-ID: 150CD8E826D73DD132572E...
    Session-ID-ctx:
    Master-Key: CCB6C3F...
    Key-Arg   : None
    PSK identity: None
    PSK identity hint: None
    SRP username: None
    TLS session ticket lifetime hint: 200 (seconds)
    TLS session ticket:
    0000 - 57 3a ...

    Start Time: 1423438080
    Timeout   : 300 (sec)
    Verify return code: 20 (unable to get local issuer certificate)
---
220-zzz.zuver.net.au ESMTP Exim 4.84 #2 Mon, 09 Feb 2015 10:27:59 +1100
220-We do not authorize the use of this system to transport unsolicited,
220 and/or bulk e-mail.

最佳答案

经过进一步测试,该问题仅出现在 PHP 5.6 中。它适用于 PHP 5.5。

原因是 PHP 5.6 中对 OpenSSL 的更改,如 this 中所述。附录,特别是:

Stream wrappers now verify peer certificates and host names by default when using SSL/TLS

因为我使用的是共享主机计划,所以 SSL 证书是针对 server1.myhosting.com 的,我通过 mail.mydomain.com 访问我的邮件服务器.不匹配,因为 SSL 证书是为 server1.myhosting.com

颁发的

将我的代码更改为

$mail->Host = 'server1.myhosting.com';

帮我修好了。

注意我的网络托管服务提供商建议我使用 server1.myhosting.com,因此这可能不适合您。

关于PHPMailer 不会使用 SSL 发送,但 Outlook 会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28389542/

相关文章:

perl - 通过 Gmail 发送电子邮件

cocoa - 在 Cocoa 中发送带有附件的电子邮件

php - 检查 MySQL 数据库中是否存在没有唯一键的电子邮件地址

java - spring boot - undertow https session 不会过期或停止 ssl 握手

PHP用外键删除记录后,检查int表

javascript - 使用多个下拉列表选择的 URL 查询字符串(最好是 PHP?)

php - 从 PHP 脚本运行 bash 脚本

java - Jetty:将 SSL 连接器配置为仅服务于特定资源

ssl - 为什么在向 sandbox.example.com 等任何子域发送请求时会出现此 SSL 错误?

php - 使用 php 和 mysqli 有多少行包含特定值