【问题标题】:Connecting to Gmail through IMAP with PHP - SSL context failed使用 PHP 通过 IMAP 连接到 Gmail - SSL 上下文失败
【发布时间】:2010-11-19 11:34:14
【问题描述】:

我正在尝试使用在 Apache 中运行的 PHP 通过 IMAP 连接到 Gmail。这是在 Ubuntu 9.04 系统上。我遇到了某种 PHP 配置问题,导致它无法正常工作。首先,这是我为 PHP 设置 IMAP 所做的:

sudo apt-get install libc-client2007b libc-client2007b-dev
sudo apt-get install php5-imap
sudo /etc/init.d/apache2 start

当我运行 phpinfo() 时,我得到以下 imap 值:

IMAP c-Client Version: 2004
SSL Support: enabled
Kerberos Support: enabled

这是我的示例代码:

<?php
$connect_to = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';
$user = 'my gmail address';
$password = 'my gmail password';

$connection = imap_open($connect_to, $user, $password)
  or die("Can't connect to '$connect_to': " . imap_last_error());

imap_close($connection);
?>

当我执行这段代码时,我得到以下输出:

Warning: imap_open() [function.imap-open]: Couldn't open stream {imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX in /var/www/clint/gmail/gmail.php on line 10
Can't connect to '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX': TLS/SSL failure for imap.gmail.com: SSL context failed

请注意,我可以从这台计算机远程登录到 imap.gmail.com:993。我还可以通过 IMAP 将 Evolution(邮件阅读器)连接到 Gmail 并毫无问题地获取邮件。所以,我认为这不是防火墙问题。我很确定我在 PHP 中的某些内容设置不正确。

有什么想法吗?

【问题讨论】:

标签: php ssl ubuntu gmail imap


【解决方案1】:

使用phpinfo() 检查您的设置,并确保您看到列出的--with-imap-ssl

【讨论】:

  • 我自己不是在构建 PHP。我已经用 apt-get 安装了它。我读过的所有博客都表明,如果我执行“apt-get install php5-imap”,那么它将起作用。也许我不得不放弃这种方法并自己编译 PHP。我今晚试试。
  • 检查 phpinfo() 会告诉你是否启用了 SSL。听起来它可能不再默认包含在内。
  • 你应该在 phpinfo 中有 openssl(我相信这就是 IMAP 所使用的):openssl OpenSSL support enabled OpenSSL Version OpenSSL 0.9.7i 14 Oct 2005
【解决方案2】:

从命令行运行你的代码,看看 php 是否会吐出我们的任何其他错误:

php -f gmail.php

在我的 Ubuntu 上我做了:

sudo apt-get install php-imap

在 php 上安装 imap 并安装系统:libc-client2007b mlock libc-client2007b mlock php-imap

那如何卸载php5重新安装干净。

【讨论】:

    【解决方案3】:

    您需要在 PHP 中启用的另一件事是OpenSSL extension。 IMAP 客户端库(带有 SSL)似乎依赖于此。

    Apache 是否启用了 OpenSSL 模块并不重要,因为在将请求移交给 PHP 之前已对其进行处理/处理。

    以下讨论主题可能有助于阐明一些观点:

    http://groups.google.com/group/comp.lang.php/browse_thread/thread/241e619bc70a8bf4/bd3ae0c6a82409bc?lnk=raot&pli=1

    【讨论】:

    • 我收到了类似的警告。你们是如何解决的。我的服务器上启用了 OpenSSL。
    【解决方案4】:

    经过长时间的努力,这对我有用:

    $ServerName = "{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox";
    

    【讨论】:

    • Windows Ultimate x64、Apache 2.2、PHP 5.2.8:{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX 有效。
    • Windows Vista x64、Apache 2.2、PHP 5.2.8:{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX 工作。
    【解决方案5】:

    如果您在 gmail 上仍然遇到此问题,请确保在您的 Google 帐户安全设置页面中启用“允许安全性较低的应用访问”。

    【讨论】:

    • 他在 6 年前遇到过 ssl 问题,而不是登录问题。
    • 我最近遇到了类似的问题,即使在尝试了此处列出的所有解决方案之后,直到我在我的谷歌帐户安全设置页面中启用“允许访问不太安全的应用程序”时才发出已解决
    【解决方案6】:

    在谷歌应用上的个人域也有同样的问题。通过更改应用程序对帐户的访问权限解决了问题。只需关注by link 并开启对帐户的访问权限。

    【讨论】:

    • 为我工作,也使用个人域(企业版 Gmail)
    【解决方案7】:

    我遇到了同样的问题。 我正在使用 windows 和 wamp,并且我的 wamp "openSSl" 扩展已启用。

    我通过以下步骤解决了这个问题。我希望这也对你有用。

    1) 通过浏览器登录到 gmail 帐户。

    2) 打开这个网址“https://www.google.com/settings/security/lesssecureapps

    3) 点击“开启”

    4) 试试下面的代码

    <?php
    
    set_time_limit(4000);
    
    
    // Connect to gmail
    //$imapPath = '{imap.gmail.com:993/imap/ssl}INBOX';
    $imapPath = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';
    $username = 'your-emai-address@gmail.com';
    $password = 'Your-password';
    
    // try to connect
    $inbox = imap_open($imapPath,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
       /* ALL - return all messages matching the rest of the criteria
        ANSWERED - match messages with the \\ANSWERED flag set
        BCC "string" - match messages with "string" in the Bcc: field
        BEFORE "date" - match messages with Date: before "date"
        BODY "string" - match messages with "string" in the body of the message
        CC "string" - match messages with "string" in the Cc: field
        DELETED - match deleted messages
        FLAGGED - match messages with the \\FLAGGED (sometimes referred to as Important or Urgent) flag set
        FROM "string" - match messages with "string" in the From: field
        KEYWORD "string" - match messages with "string" as a keyword
        NEW - match new messages
        OLD - match old messages
        ON "date" - match messages with Date: matching "date"
        RECENT - match messages with the \\RECENT flag set
        SEEN - match messages that have been read (the \\SEEN flag is set)
        SINCE "date" - match messages with Date: after "date"
        SUBJECT "string" - match messages with "string" in the Subject:
        TEXT "string" - match messages with text "string"
        TO "string" - match messages with "string" in the To:
        UNANSWERED - match messages that have not been answered
        UNDELETED - match messages that are not deleted
        UNFLAGGED - match messages that are not flagged
        UNKEYWORD "string" - match messages that do not have the keyword "string"
        UNSEEN - match messages which have not been read yet*/
    
    // search and get unseen emails, function will return email ids
    $emails = imap_search($inbox,'UNSEEN');
    
    $output = '';
    
    foreach($emails as $mail) {
    
        $headerInfo = imap_headerinfo($inbox,$mail);
    
        $output .= $headerInfo->subject.'<br/>';
        $output .= $headerInfo->toaddress.'<br/>';
        $output .= $headerInfo->date.'<br/>';
        $output .= $headerInfo->fromaddress.'<br/>';
        $output .= $headerInfo->reply_toaddress.'<br/>';
    
        $emailStructure = imap_fetchstructure($inbox,$mail);
        //var_dump($emailStructure->parts);
        if(isset($emailStructure->parts)) {
             $output .= imap_body($inbox, $mail, FT_PEEK);
        } else {
            //    
        }
       echo $output;
       $output = '';
    }
    
    // colse the connection
    imap_expunge($inbox);
    imap_close($inbox);
    
    
    ?>
    

    祝你好运。 :)

    【讨论】:

    【解决方案8】:
    1. 首先,在您的 Gmail 帐户中启用安全性较低的应用:https://myaccount.google.com/lesssecureapps

    2. 使用此配置创建 IMAP 连接:

      $imap_connection = imap_open('{imap.gmail.com:993/imap/ssl/novalidate-
      cert}INBOX', 'YOUR GMAIL USER', 'YOUR GMAIL PASSWORD');
      

    注意:INBOX 是您的主要收件箱,例如,您可以通过以下方式访问已发送的项目:INBOX.Sent 在您的连接中。

    【讨论】:

      【解决方案9】:

      特定于 Gmail 的 IMAP 服务器:

      Google 已开始为不提供 SNI 主机名的 TLSv13 连接返回无效证书。 当针对升级的 OpenSSL (libssl) 版本构建 php-imap 扩展时,这会导致回归。 这些问题已在此处报告:

      后者是最近才出现的,我已将其提交给 Ubuntu 维护人员,以使其对他们可见,并希望吸引更多的目光,因为它影响了 LTS 版本的 Ubuntu Bionic。

      请对它们进行投票和/或评论,以提高知名度。 还请记住:如果您的这个答案有用并且它也得到了足够的支持,那么上述问题将会得到更多的关注。

      与此同时,在我看来,在修复错误之前唯一的解决方法是在调用 imap_open() 时使用 /novalidate-cert 标志禁用证书验证,这是一个安全风险。

      【讨论】:

        【解决方案10】:

        我是 Arvind Gondaliya,你是对的,但如果不太安全的区域关闭,则可以获取 gmail 邮件。

        【讨论】:

          猜你喜欢
          • 2017-03-30
          • 2015-11-20
          • 2016-09-01
          • 2012-08-30
          • 2014-05-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-08-30
          相关资源
          最近更新 更多