使用 PHP 通过 IMAP 连接到 Gmail - SSL 上下文失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1274911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:50:38  来源:igfitidea点击:

Connecting to Gmail through IMAP with PHP - SSL context failed

phpsslubuntugmailimap

提问by Clint Miller

I'm trying to connect to Gmail through IMAP with PHP running in Apache. This is on an Ubuntu 9.04 system. I've got some sort of PHP configuration issue that is keeping this from working. First, here's what I did to setup IMAP for PHP:

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

When I run phpinfo(), I get the following imap values:

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

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

Here's my sample code:

这是我的示例代码:

<?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);
?>

When I execute this code, I get the following output:

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

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

Note that I can telnet to imap.gmail.com:993 from this computer. I can also hookup Evolution (mail reader) to Gmail through IMAP and fetch mail without problems. So, I don't think this is a firewall issue. I'm pretty sure I've got something in PHP not setup correctly.

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

Any ideas?

有任何想法吗?

采纳答案by Jordan S. Jones

One more additional thing you need enabled in PHP, is the OpenSSL extension. It appears that the IMAP Client library (with SSL) depends on this.

您需要在 PHP 中启用的另一项附加功能是OpenSSL 扩展。看来 IMAP 客户端库(带 SSL)依赖于此。

It doesn't matter if Apache has the OpenSSL module enabled as this is processed/handled before the request is handed off to PHP.

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

The following discussion thread may help shed some light:

以下讨论线程可能有助于阐明:

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

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

回答by Supriya

This has worked for me after a long effort:

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

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

回答by Adnan Ahmad

I was facing same problem. I am using windows and wamp and my wamp "openSSl" extension is enabled.

我面临同样的问题。我正在使用 windows 和 wamp,并且启用了 wamp 的“openSSl”扩展。

I removed this issue by using following steps.I hope this will work for you too.

我使用以下步骤删除了这个问题。我希望这对你也有用。

1) Logged in via browser to gmail account.

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

2) Open this url "https://www.google.com/settings/security/lesssecureapps"

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

3) Click at "turn on"

3)点击“开启”

4) try following code

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 = '[email protected]';
$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);


?>

BEST OF LUCK. :)

祝你好运。:)

回答by Anton Danilov

had the same problem with personal domain on google apps. Problem was solved by changing application access to account. Simply follow by linkand turn ON access to account.

谷歌应用程序上的个人域也有同样的问题。通过更改对帐户的应用程序访问权限解决了问题。只需点击链接并打开对帐户的访问。

回答by amontero

Specific to Gmail's IMAP servers:

特定于 Gmail 的 IMAP 服务器:

Google has started returning invalid certificates for TLSv13 connections which do not provide SNI hostname. This causes a regression when building php-imap extension against an upgraded OpenSSL (libssl) version. The issues have already been reported here:

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

The later is quite recent, and I've filed it to Ubuntu maintainers to make it visible to them and hopefully attract more eyes, since it affects Ubuntu Bionic which is a LTS version.

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

Please, vote and/or comment on them to give better visibility. Also remember: if you this answer useful and it is also upvoted enough, the above issues will get still more visibility.

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

In the meantime, looks like to me that the only workaround until the bug is fixed is to disable certificate validation by using the /novalidate-certflag when calling imap_open(), which is a security risk.

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

回答by Derick Schoonbee

Run your code from the command line and see if php spits our any other errors:

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

php -f gmail.php

On my Ubuntu I did:

在我的 Ubuntu 上,我做了:

sudo apt-get install php-imap

to install imap on php and the system installed: libc-client2007b mlock libc-client2007b mlock php-imap

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

Then how about uninstalling php5 and re-installing cleanly.

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

回答by Esteban

  1. First, enable less secure apps in your Gmail account: https://myaccount.google.com/lesssecureappsenter image description here

  2. Use this configuration to create IMAP connection:

    $imap_connection = imap_open('{imap.gmail.com:993/imap/ssl/novalidate-
    cert}INBOX', 'YOUR GMAIL USER', 'YOUR GMAIL PASSWORD');
    
  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');
    

Note: INBOX is your main imbox, for example you can acces to sent items with: INBOX.Sent in your connection.

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

回答by abdul01

If you're still having issues with this on gmail, make sure to enable "Allow access for less secure apps" in your google accounts security settings page.

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

回答by ayman

Check your setup with phpinfo()and ensure you see --with-imap-ssllisted.

检查您的设置phpinfo()并确保您看到--with-imap-ssl列出的内容。

回答by user3076732

I am Arvind Gondaliya, You are right but if Less secure area off then to get gmail mail.

我是 Arvind Gondaliya,您说得对,但如果关闭安全区域较低,则可以接收 gmail 邮件。