2009-08-13 134 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連接。