20

我正在尝试使用在 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 中有一些设置不正确。

有任何想法吗?

4

10 回答 10

11

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

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

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

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

于 2009-08-17T21:24:51.583 回答
10

This has worked for me after a long effort:

$ServerName = "{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox";
于 2009-11-09T16:56:07.503 回答
8

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


?>

祝你好运。:)

于 2016-05-27T08:28:42.593 回答
3

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

于 2015-10-01T14:43:02.553 回答
2

特定于 Gmail 的 IMAP 服务器:

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

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

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

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

于 2019-06-26T15:16:35.373 回答
1

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

php -f gmail.php

在我的 Ubuntu 上,我做了:

sudo apt-get install php-imap

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

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

于 2009-08-16T18:15:56.410 回答
0

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

于 2009-08-14T19:20:12.103 回答
0
  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');
    

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

于 2017-05-26T21:39:56.280 回答
0

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

于 2020-01-15T06:57:09.297 回答
0

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

于 2015-09-22T13:19:30.073 回答