2009-08-13 132 views
19

我试图通过IMAP与运行在Apache中的PHP连接到Gmail。这是在Ubuntu 9.04系统上。我有一些PHP配置问题,使这从工作。首先,这里就是我所做的设置IMAP的PHP:通过使用PHP通过IMAP连接到Gmail - SSL上下文失败

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没有正确设置。

任何想法?

+0

注:作者解决了他的问题在约旦链接的讨论,请参阅http://groups.google.com/group/comp.lang.php/browse_thread/thread/241e619bc70a8bf4/bd3ae0c6a82409bc#e820c4fc3ee78ee3 – 2009-08-23 13:06:02

+2

@Clint:请回答或根据您找到的解决方案编辑您自己的问题 – 2009-08-23 13:14:26

回答

10

您需要在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

+0

现在这个权利挣扎; +1的OpenSSL技巧。 – Ben 2010-12-29 02:41:35

+2

我收到类似的警告。人们如何解决它。 OpenSSL在我的服务器上启用。 – 2013-09-13 08:52:11

0

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

+0

我不是自己构建PHP。我用apt-get安装了它。我读过的所有博客都表明,如果我执行“apt-get install php5-imap”,那么它将起作用。也许我必须放弃这种方法并自己编译PHP。我今晚会尝试。 – 2009-08-14 21:42:49

+0

检查phpinfo()会告诉你SSL是否启用。这听起来像它可能不再被默认包含在内。 – TheJacobTaylor 2009-08-20 04:57:25

+0

您应该具有的phpinfo OpenSSL的(我相信这是什么IMAP使用): OpenSSL的 OpenSSL的支持\t启用 OpenSSL的版本\t的OpenSSL 0.9.7i 2005年10月14日通过 – TheJacobTaylor 2009-08-20 04:59:24

1

在命令行中运行你的代码,看看PHP的吐出了其它的错误:

php -f gmail.php 

在我的Ubuntu我所做的:

sudo apt-get install php5-imap 

而且系统安装功能:libc-client2007b m锁libc-client2007b mlock php5-imap

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

9

这已经经过长期努力,为我工作:

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

Windows Ultimate x64,Apache 2.2,PHP 5.2.8:'{imap.gmail.com:993/imap/ssl/novalidate-cert} INBOX'工作。 – Ben 2010-12-29 05:20:13

+0

Windows Vista x64,Apache 2.2,PHP 5.2.8:'{imap.gmail.com:993/imap/ssl/novalidate-cert} INBOX'工作。 – Ben 2010-12-29 05:21:13

0

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

+0

6年前,他遇到了ssl问题,而不是登录问题。 – Max 2015-09-24 04:32:36

+0

我最近有一个类似的问题,甚至在尝试所有在这里列出的d解决方案后,直到当我在我的谷歌帐户安全设置页面中启用“允许访问不太安全的应用程序”时,d问题得到解决 – abdul01 2015-09-24 10:04:38

3

与Google应用上的个人域名有相同的问题。通过更改应用程序对帐户的访问来解决问题。只需按照by link并开启对账户的访问。

+0

为我工作,也使用个人域名(Gmail用于商业用途) – Bryan 2016-05-02 08:47:38

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 = '[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); 


?> 

好运。 :)

+0

如果其他人想知道哪个答案真的有效。我尝试了这一点,它工作。 – tormuto 2016-06-03 08:40:55

+0

打开此URL“https://www.google.com/settings/security/lesssecureapps”。这件事解决了我的问题。 – 2016-12-22 04:31:22

0
  1. 首先,在您的Gmail帐户启用安全性较低的应用程序:https://myaccount.google.com/lesssecureappsenter image description here

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

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

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