PEAR Mail 无法连接到 Gmail SMTP,无法连接到套接字

PEAR Mail unable to connect to Gmail SMTP, failed to connect to socket

事实

我正在使用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.dllerror.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

我 googled 只是第一个警告的 "fsockopen(): SSL operation failed with code 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)]

所以我 googled code: 534, response: 5.7.14 并最终 here,按照 emgh3i 的第一个答案的说明,启用安全性较低的连接并允许访问我的 google 帐户

它现在运行良好。

您的 host 配置不应包含该协议。它失败的原因是因为它可能试图在 ssl://smtp.gmail.com 上执行 DNS 查找但失败了。

改变

'host' => 'ssl://smtp.gmail.com',

'host' => 'smtp.gmail.com',

在开始之前,让我先说明一下,您的服务器和 google 服务器之间有许多可能的解决方案和结果,因此这些可能适用于不同的人,也可能不适用于不同的人。

1) SMTP 不是很安全,因此 Google 可能会拒绝您的请求。我在 6 个月前遇到了这个问题,解决方案是在 'myaccount.google.com'

下启用不安全的应用程序

2)如果这对你不起作用那么你可以考虑切换协议。

来自

'host' => 'ssl://smtp.gmail.com',

'host' => 'tls://smtp.gmail.com:587';

您的代码是正确的

我尝试测试我的 gmail 帐户。邮件发送成功。

检查您的套接字连接

<?php

error_reporting(E_ALL);

var_dump(fsockopen("ssl://smtp.gmail.com", 465, $errno, $errstr));
var_dump($errno);
var_dump($errstr);

resource(4) of type (stream)

int(0)

string(0) ""

几个调试步骤:

1。检查 phpinfo

我建议检查phpinfo() 以检查是否启用了所有模块。检查邮件,fsocketopen。

2。启用调试标志

启用debug 标记来检查究竟是什么问题。如下图。

$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'debug' => true,
        'pipelining' => true,
        'username' => 'xxx@gmail.com',
        'password' => 'xxx'
    ));

在我的机器上 运行 以上代码之后,我得到了以下响应。问题可能与您的不同。但是调试帮助了我。因为我启用了 2FA,所以它给了我错误。我也收到一封邮件,说我的登录被阻止了。

DEBUG: Recv: 220 smtp.gmail.com ESMTP s65sm4891344pfi.36 - gsmtp DEBUG: Send: EHLO localhost DEBUG: Recv: 250-smtp.gmail.com at your service, [110.227.210.84] DEBUG: Recv: 250-SIZE 35882577 DEBUG: Recv: 250-8BITMIME DEBUG: Recv: 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH DEBUG: Recv: 250-ENHANCEDSTATUSCODES DEBUG: Recv: 250-PIPELINING DEBUG: Recv: 250-CHUNKING DEBUG: Recv: 250 SMTPUTF8 DEBUG: Send: AUTH LOGIN DEBUG: Recv: 334 VsadfSFcm5hbWU6 DEBUG: Send: cGF0ZWwuZ29wYhkafdaASFnbWFpbC5jb20= DEBUG: Recv: 334 UGFzc3dvcmQ6 DEBUG: Send: OWwzMy5zaHlAbTE4 DEBUG: Recv: 534-5.7.14 Please log in via your web browser and DEBUG: Recv: 534-5.7.14 then try again. DEBUG: Recv: 534-5.7.14 Learn more at DEBUG: Recv: 534 5.7.14 https://support.google.com/mail/answer/78754 s65sm4891344pfi.36 - gsmtp DEBUG: Send: RSET DEBUG: Send: QUIT DEBUG: Recv: 250 2.1.5 Flushed s65sm4891344pfi.36 - gsmtp DEBUG: Recv: 221 2.0.0 closing connection s65sm4891344pfi.36 - gsmtp
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 s65sm4891344pfi.36 - gsmtp)]

更新:

您的问题看起来 PHP 甚至无法连接到 gmail 服务器。

"Use the Gmail SMTP Server" section of this guide says you need to enable "Less secure apps".

Been Kyung-yoong 是迄今为止唯一为解决问题做出有意义贡献的人(+1 Been!)。我可以确认他的结果。我建议你也试试。您当前正在尝试调试相当复杂的组件堆栈:

Been 正在为你做你的工作 - 作为发布问题的人 - 应该创建一个 Minimal, Complete, and Verifiable example

这也有望提供更有意义的诊断信息。

失败的最可能原因是:

  • 您所在的主机 运行 无法将传出连接路由到互联网(但由于您似乎使用的是台式电脑,我想您现在可能已经注意到了这一点)
  • 代码 运行 在安全沙箱中(但 MSWindows 并没有这样的东西)
  • 主机无法解析主机名(参见关于路由的第一点)
  • 主机能够连接但无法验证证书

因此您可能会考虑这个更详细的测试脚本实现:

 <?php

 error_reporting(E_ALL);

 print "DNS\n";
 var_dump(getmxrr('gmail.com',$result));
 var_dump($result);
 $use_ip=gethostbyname($result[0]);
 print "IPV4 address = $use_ip\n";

 print "\nIf you've got this far without errors then problem is with your SSL config\n";
 $calocns=openssl_get_cert_locations();
 if (count($calocns)) {
     print "Check you've got your cacerts deployed in one of the following locations\n";
     foreach ($calocns as $k=>$v) print "$k = $v\n";
 } else {
     print "You've not configured your openssl installation on this host\n";
 }

 print "\nIf all good so far, then this bit should work....\n";
 print "fsockopen\n";
 var_dump(fsockopen("ssl://smtp.gmail.com", 465, $errno, $errstr, 3.0));
 var_dump($errno);
 var_dump($errstr);

应该给你这样的回复:

 DNS
 bool(true)
 array(5) {
   [0]=>
   string(31) "alt1.gmail-smtp-in.l.google.com"
   [1]=>
   string(31) "alt2.gmail-smtp-in.l.google.com"
   [2]=>
   string(31) "alt4.gmail-smtp-in.l.google.com"
   [3]=>
   string(26) "gmail-smtp-in.l.google.com"
   [4]=>
   string(31) "alt3.gmail-smtp-in.l.google.com"
 }
 IPV4 address = 74.125.131.26

 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 = /usr/lib/ssl/cert.pem
 default_cert_file_env = SSL_CERT_FILE
 default_cert_dir = /usr/lib/ssl/certs
 default_cert_dir_env = SSL_CERT_DIR
 default_private_dir = /usr/lib/ssl/private
 default_default_cert_area = /usr/lib/ssl
 ini_cafile =
 ini_capath =

 If all good so far, then this bit should work....
 fsockopen
 resource(4) of type (stream)
 int(0)
 string(0) ""

鉴于我们无法复制您的错误,我们无法给出问题所在的明确答案 - 但我的猜测是 you haven't configure openSSL.

当出现故障且我们不知道原因时,我们必须进行调试。所以在这里我没有给出答案,而是要求你执行一些测试

  1. 确认系统与互联网的连接:打开 cmd 终端并输入

    ping smtp.gmail.com
    
  2. 确认防火墙:在cmd终端输入以下内容

    telnet smtp.gmail.com 465
    
  3. 确认 php 设置:在 cmd 终端输入 php -a 并在 php 提示符下执行(复制/粘贴然后按回车键)以下代码。

    $result = fsockopen('ssl://smtp.gmail.com', 465, $error_no, $error_message, 5);
    if ($result === false) {
      echo "error no: $error_no error message: $error_message";
      echo print_r($result, true);
    } else {
      echo 'success';
    }
    
  4. 确认 Pear Mail 库和 Gmail SMTP 访问:再次在 cmd 和 php 提示符下 php -a 执行您自己的代码(如您在此线程中发布的那样)

让我们知道它在哪里中断,以及错误是什么。只有在那之后我们才能提供帮助

在PHP5.3中,别人给的"less secure apps,"登录GoogleGmail账号需要激活的提示,解决了我所有的问题

在 PHP 7.2 中,我必须做更多:将 'auth' 设置为 PLAIN,然后为 ssl socket_options 添加 "verify_peer" 和 verify_peer_name,如下所示:

$mail= Mail::factory('smtp', array('host' => 'ssl://smtp.gmail.com',
                                   'port' => '465',
                                   'auth' => 'PLAIN',
                                   'socket_options' => array('ssl' => array('verify_peer' => false,
                                                                            'verify_peer_name' => false)),
                                   'username' => 'someAccount@gmail.com',
                                   'password' => 'myPassword'
));

这可能与问题没有直接关系,但对于通过远程服务器使用 Pear SMTP 发送电子邮件有问题的任何人来说,真正的问题不是防火墙配置,而是 SELINUX。如果它是打开的,这是默认设置,您可能会花几天时间想知道为什么权限被拒绝。只需告诉 selinux 允许 apache 发送电子邮件:

setsebool -P httpd_can_sendmail 1

我希望这能为某人省去麻烦。