我阅读了很多解释如何使用 smtp发送电子邮件的文章,例如从 PHP 页面使用 GMail SMTP 服务器发送电子邮件 我有一个云服务器和一个本地主机设置,两者都是相同的(Centos 6,只安装了 httpd,php和 mysql 通过 yum)。iptables 是一样的:输出端口打开,只输入 22 和 80。
我安装了 pear Mail 和 pear Net_SMTP 与 smtp.gmail.com 一起使用。phpinfo(); 显示 openssl 已启用。
我正在使用这段代码:
<?php require_once "Mail.php";
$from = "<from.gmail.com>";
$to = "<to.yahoo.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "<myaccount.gmail.com>";
$password = "password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?> <!-- end of php tag-->
在我的本地主机服务器上,脚本工作,我在我的 gmail 帐户中收到了电子邮件。但是在我的云服务器上,使用相同的脚本,我收到了这个错误:(使用调试)
ErrorFailed to connect to ssl://smtp.gmail.com:465 [SMTP: Failed to connect socket: Permission denied (code: -1, response: )]DEBUG: Send: QUIT
我搜索了很多,我看到有人说当 465 端口被阻塞时会发生错误。
所以我查看了我的 iptables、我的连接等,并打开了 OUTPUT 的端口 465。
我做了一个测试: openssl s_client -verify 0 -connect smtp.gmail.com:465 我得到了这个结果:
verify depth is 0
CONNECTED(00000003)
depth=2 C = US, O = Equifax, OU = Equifax Secure Certificate Authority
verify return:1
depth=1 C = US, O = Google Inc, CN = Google Internet Authority
verify return:1
depth=0 C = US, ST = California, L = Mountain View, O = Google Inc, CN = smtp.gmail.com
verify return:1
---
Certificate chain
0 s:/C=US/ST=California/L=Mountain View/O=Google Inc/CN=smtp.gmail.com
i:/C=US/O=Google Inc/CN=Google Internet Authority
1 s:/C=US/O=Google Inc/CN=Google Internet Authority
i:/C=US/O=Equifax/OU=Equifax Secure Certificate Authority
---
Server certificate
-----BEGIN CERTIFICATE-----
MIIDWzCCAsSgAwIBAgIKFeQVggADAAA7NjANBgkqhkiG9w0BAQUFADBGMQswCQYD
VQQGEwJVUzETMBEGA1UEChMKR29vZ2xlIEluYzEiMCAGA1UEAxMZR29vZ2xlIElu
dGVybmV0IEF1dGhvcml0eTAeFw0xMTExMTgwMTU3MTdaFw0xMjExMTgwMjA3MTda
MGgxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1N
b3VudGFpbiBWaWV3MRMwEQYDVQQKEwpHb29nbGUgSW5jMRcwFQYDVQQDEw5zbXRw
LmdtYWlsLmNvbTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAuK+t5ZRq6c3K
kWPwLuIcPa6DgiBURaQK9akP4OBoXKJ6bqYIQWsS4C3RgnOaGaDENadxHSNZ5Qpl
Vqg2S54N54SM5OXwOq0NtrqdlbhgigB53TZouiJvnLDxxIexSOn2Gx1qyZF2z8Ii
MoUhHuStWgW5YoOHje8z6K9xQdYkQp0CAwEAAaOCASwwggEoMB0GA1UdDgQWBBTs
OL4jbtJ5l8B6/eoEvv30KEiTrjAfBgNVHSMEGDAWgBS/wDDr9UMRPme6npH7/Gra
42sSJDBbBgNVHR8EVDBSMFCgTqBMhkpodHRwOi8vd3d3LmdzdGF0aWMuY29tL0dv
b2dsZUludGVybmV0QXV0aG9yaXR5L0dvb2dsZUludGVybmV0QXV0aG9yaXR5LmNy
bDBmBggrBgEFBQcBAQRaMFgwVgYIKwYBBQUHMAKGSmh0dHA6Ly93d3cuZ3N0YXRp
Yy5jb20vR29vZ2xlSW50ZXJuZXRBdXRob3JpdHkvR29vZ2xlSW50ZXJuZXRBdXRo
b3JpdHkuY3J0MCEGCSsGAQQBgjcUAgQUHhIAVwBlAGIAUwBlAHIAdgBlAHIwDQYJ
KoZIhvcNAQEFBQADgYEAQiMlHuQLRFqR10UsSg5WTNe3vagbdnBLAkdhvAf90B5a
9beBxJH2/ylTSIGfD2uceAqzcsQe6Ouy4C9r3rz86qA1dhdtIcPg6uoZb+E2qhE5
UaOJOPO4rHInX9kscBxh+baHbpBMh+ch6v5L8plss8hd0id8C4g10YKzwcgPYlQ=
-----END CERTIFICATE-----
subject=/C=US/ST=California/L=Mountain View/O=Google Inc/CN=smtp.gmail.com
issuer=/C=US/O=Google Inc/CN=Google Internet Authority
---
No client certificate CA names sent
---
SSL handshake has read 1850 bytes and written 299 bytes
---
New, TLSv1/SSLv3, Cipher is RC4-SHA
Server public key is 1024 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
SSL-Session:
Protocol : TLSv1
Cipher : RC4-SHA
Session-ID: 55E90A8854BB04C962AB8AD7D231C89291E62B28EC93F4189CFD512B2EFD43B6
Session-ID-ctx:
Master-Key: C454B3ED7E5C522B745F0E2EBF45BDEADFAD2CE29ECE945C1CA6EBA1629921577FEECFF968D896E39CF4E0057731CD26
Key-Arg : None
Krb5 Principal: None
PSK identity: None
PSK identity hint: None
TLS session ticket lifetime hint: 100800 (seconds)
TLS session ticket:
0000 - 75 be ab 14 ff bf e3 74-61 4f 98 e6 ec 58 ae ab u......taO...X..
0010 - 50 2f 20 f5 12 14 ae b5-11 dd 2c c1 9e 99 36 b1 P/ .......,...6.
0020 - 9a 66 5b 76 15 c8 0e 7f-07 ce ce e1 4c b3 f4 12 .f[v........L...
0030 - 38 c8 43 2d a2 c7 f9 62-17 4f da 82 4f 4b 12 93 8.C-...b.O..OK..
0040 - af 31 9e d5 90 8d 3e 4c-06 d6 73 30 fb b6 95 80 .1....>L..s0....
0050 - 59 1c 65 e3 d3 51 2e a7-48 15 11 ba 9f 72 89 12 Y.e..Q..H....r..
0060 - 9a 68 63 df 65 22 0f cb-60 b7 cf 3c b4 c6 f9 92 .hc.e"..`..<....
0070 - a0 c1 34 d7 06 31 97 ef-e6 8a bf b8 14 d9 72 b0 ..4..1........r.
0080 - 13 d9 dd df ce 48 a1 83-74 53 d6 fe b0 5a 53 a1 .....H..tS...ZS.
0090 - ee d0 9e b3 ....
Start Time: 1335462088
Timeout : 300 (sec)
Verify return code: 0 (ok)
---
所以,我认为这个问题与“阻塞端口”无关。我也试过587端口...
我向我的主机发送了一条消息,询问 ISP 是否可以阻止这些端口,他们告诉我他们不会阻止任何端口,客户端(我)可以完全控制服务器。
我不知道还有什么会导致此错误以及我现在可以做什么。也许我没有看到一些配置文件。
任何人都有一些建议可以帮助我或建议我可以做更多的测试来获取更多关于我为什么会收到该错误并解决此问题的信息?
谢谢