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

PHPMailer won't send with SSL but Outlook will

我在尝试使用 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

这是我的 tried/observed:

我真的不知道从哪里开始寻找,因为我可以使用 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';

帮我修好了。

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