2010-08-03 102 views

回答

1

IMAP解決方案

/* connect to gmail */ 
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX'; 
$username = '[email protected]'; 
$password = 'davidwalsh'; 

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

/* grab emails */ 
$emails = imap_search($inbox,'ALL'); 

/* if emails are returned, cycle through each... */ 
if($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>'; 
    } 

    echo $output; 
} 

/* close the connection */ 
imap_close($inbox); 

提取附件您必須添加到它連接控制,您可以請閱讀本文,因爲它是源代碼http://davidwalsh.name/gmail-php-imap

+1

-1這是反芻代碼,並沒有解決有關附件的原始問題(「如何使用php下載gmail帳戶的附件?」)。你說「添加附件控制」但是......怎麼樣? – Ben 2010-12-29 05:29:09

+0

@Steve:從這一點你可以簡單地使用PHP文檔,用幾行代碼下載你想要的附件,如果你不相信,那麼看到綠色標記 – Svisstack 2010-12-29 12:49:32

+0

對不起Svisstack,我必須同意@Ben:the代碼不會下載附件。您建議的鏈接不會提供任何有關下載附件的代碼。 – 2017-06-12 08:13:16

3

你可以使用PHP的POP或IMAP客戶端,並獲取郵件,並從

+3

imap比pop更好,gmail支持這個 – Svisstack 2010-08-03 10:03:00

3

正如其他回答者所述,建議使用IMAP解決方案,並且可以在http://davidwalsh.name/gmail-php-imap。確保檢查您的防火牆,並在您的PHP安裝中啓用OpenSSL和IMAP。

http://www.electrictoolbox.com/extract-attachments-email-php-imap/關於如何使用imap_fetchstructure分析每個電子郵件的結構具有優秀的代碼參考。在每條消息的結構中都有附件。用戶提供的註釋http://www.php.net/manual/en/function.imap-fetchstructure.php特別有用(正如文檔)。

基本上,通過結構部件必須

  • 確定哪些是附件
  • 解碼適當。

不同的電子郵件客戶端將有稍微不同的結構,但它不是一點努力都不難。然後可以處理數據,但您可以選擇。