事实
我正在使用 PEAR Mail,我想使用 gmail 的 SMTP 发送邮件。我拥有 Apache/2.4.27 (Win64) PHP/7.2.0beta3、PEAR 1.10.15、Mail 1.4.1、Net_SMTP 1.8.0 和 Net_Socket 1.2.2。
我进入了 php.ini
,添加了 extension = php_openssl.dll
。 error.log
没有显示与 SSL 相关的错误。
这里是代码:
require_once "Mail.php";
$from = '<slevin@gmail.com>';
$to = '<slevinkelevra@gmal.com>';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'slevinmail@gmail.com',
'password' => 'mypassword'
));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo('<p>' . $mail->getMessage() . '</p>');
} else {
echo('<p>Message successfully sent!</p>');
}
问题
我遇到了这个错误:
Failed to connect to ssl://smtp.gmail.com:465 [SMTP: Failed to connect socket: fsockopen(): unable to connect to ssl://smtp.gmail.com:465 (Unknown error) (code: -1, response: )]
但我不知道该怎么办,我在谷歌上搜索,但越来越困惑。
请指导如何解决此问题。谢谢。
更新
按照symcbean的说明,我得到了以下结果:
bool(true)
array(5) {
[0]=> string(31) "alt3.gmail-smtp-in.l.google.com"
[1]=> string(26) "gmail-smtp-in.l.google.com"
[2]=> string(31) "alt4.gmail-smtp-in.l.google.com"
[3]=> string(31) "alt1.gmail-smtp-in.l.google.com"
[4]=> string(31) "alt2.gmail-smtp-in.l.google.com" }
IPV4 address = 64.233.188.27
If you've got this far without errors then problem is with your SSL config
Check you've got your cacerts deployed in one of the following locations
default_cert_file = C:\Program Files\Common Files\SSL/cert.pem
default_cert_file_env = SSL_CERT_FILE
default_cert_dir = C:\Program Files\Common Files\SSL/certs
default_cert_dir_env = SSL_CERT_DIR
default_private_dir = C:\Program Files\Common Files\SSL/private
default_default_cert_area = C:\Program Files\Common Files\SSL
ini_cafile =
ini_capath =
If all good so far, then this bit should work....
fsockopen
Warning: fsockopen(): SSL operation failed with code 1. OpenSSL Error messages: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed in C:\Apache24\htdocs\phptest2.php on line 28
Warning: fsockopen(): Failed to enable crypto in C:\Apache24\htdocs\phptest2.php on line 28
Warning: fsockopen(): unable to connect to ssl://smtp.gmail.com:465 (Unknown error) in C:\Apache24\htdocs\phptest2.php on line 28
bool(false) int(0) string(0) ""
第28行是这一行:var_dump(fsockopen("ssl://smtp.gmail.com", 465, $errno, $errstr, 3.0));
再次感谢
更新#2
我仅谷歌了第一个警告的“fsockopen():SSL操作失败,代码为1。”
最终在这里找到了答案。我更改了AVG的邮件端口,就像答案中所说的那样。 symcbean的代码运行没有错误,但我的代码回复了以下内容
mail error : authentication failure [SMTP: Invalid response code received from server (code: 534, response: 5.7.14 Please log in via your web browser and 5.7.14 then try again. 5.7.14 Learn more at 5.7.14 https://support.google.com/mail/answer/78754 c1sm1243434wre.84 - gsmtp)]
因此,我谷歌搜索了code: 534,response: 5.7.14
,并最终在这里找到了答案,按照emgh3i的第一个答案的说明启用了不太安全的连接,并允许访问我的Google帐户
现在它完美地工作。
openssl client
以了解如何建立测试连接。如果失败了,可能是因为有防火墙规则。如果只是从Apache/PHP失败了,可能是SELinux/AppArmor等问题。 - mariophpinfo()
函数会显示有关 OpenSSL 的信息吗? - Don't Panicopenssl客户端
以建立测试连接? - slevinopenssl.cafile
和opensll.capath
的值。 - slevin