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

我试图通过在Apache中运行的PHP通过IMAP连接到Gmail。 这是在Ubuntu 9.04系统上。 我有一些PHP的configuration问题,使这从工作。 首先,这是我为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 

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

有任何想法吗?

您需要在PHP中启用另外一件事,就是OpenSSL扩展 。 看起来,IMAP客户端库(使用SSL)依赖于此。

如果Apache已经启用了OpenSSL模块,那么在请求交给PHP之前处理/处理它并不重要。

以下讨论主题可能有助于阐明:

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

经过漫长的努力,这已经为我工作了:

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

我面临同样的问题。 我正在使用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); ?> 

最好的运气。 🙂

在Google应用上有与个人域名相同的问题。 通过更改应用程序对帐户的访问来解决问题。 只需按链接并打开账户访问权限。

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

 php -f gmail.php 

在我的Ubuntu上,我做到了:

 sudo apt-get install php5-imap 

并安装系统:libc-client2007b mlock libc-client2007b mlock php5-imap

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

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

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

  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是您的主要imbox,例如,您可以访问发送的项目:INBOX.Sent在您的连接。