2012-10-31 39 views
0

我想知道如何從Gmail帳戶取未讀電子郵件。我有一個Gmail帳戶,我希望來自Gmail帳戶的所有未讀電子郵件都通過PHP來到我的網站。我一直在執行各種代碼,但他們沒有幫助我。請幫我解決這個問題。想要從Gmail中取未讀電子郵件

我有一個代碼,我用來從gmail中獲取郵件,但是這些郵件並沒有被獲取。

<?php 
/* connect to gmail */ 

$hostname = '{imap.gmail.com:993/imap/ssl}INBOX'; 
$username = '[email protected]'; 
echo $password = 'abcd'; 

/* try to connect */ 
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error()); 
echo "got inbox"; 
/* grab emails */ 
$emails = imap_search($inbox); 

/* if emails are returned, cycle through each... */ 
if($emails) { 
    echo "got emails"; 
    /* begin output var */ 
    $output = ''; 

    /* put the newest emails on top */ 
    rsort($emails); 

    /* for every email... */ 
    foreach($emails as $email_number) { 
    /* get information specific to this email */ 
    $overview = imap_fetch_overview($inbox,$email_number,0); 
    $message = imap_fetchbody($inbox,$email_number,2); 

    /* output the email header information */ 


    $output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">'; 
    $output.= '<span class="subject">'.$overview[0]->subject.'</span> '; 
    $output.= '<span class="from">'.$overview[0]->from.'</span>'; 
    $output.= '<span class="date">on '.$overview[0]->date.'</span>'; 
    $output.= '</div>'; 

    /* output the email body */ 
    $output.= '<div class="body">'.$message.'</div>'; 
    break; 
    } 

    echo $output; 
} 

/* close the connection */ 
imap_close($inbox); 
?> 
+3

是否發生錯誤?如果是這樣,那是什麼? – Tim

+1

提供有關您收到的錯誤的更多信息,以幫助人們給您更好的答案 –

+0

我沒有收到任何錯誤,所以我很迷惑,我錯了。 – user1788261

回答

1

使用imap_open

$inbox = imap_open("{imap.gmail.com:993/imap/ssl}INBOX",$username,$password); 

看到一個例子here

+0

哦!我一定是失明的。 –

+0

我不是問你我是在問這種問題,責備OP。沒關係問題被標記。 – NewUser

+0

沒問題。我只是覺得,我也應該注意到它。 ;) –

2

用於提取未讀郵件做:

$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error()); 
$unread_msgs = imap_search($inbox, 'UNSEEN'); 

你可以參考相應manual以及其他選項。