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

2022-01-23 00:00:00 ubuntu imap ssl gmail php

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

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:

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

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

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

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

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

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

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

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.

有什么想法吗?

推荐答案

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

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.

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

It doesn't matter if Apache has the OpenSSL module enabled as this is processed/handled before the request is handed off to 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

相关文章