2010-08-03 88 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特别有用(正如文档)。

基本上,通过结构部件必须

  • 确定哪些是附件
  • 解码适当。

不同的电子邮件客户端将有稍微不同的结构,但它不是一点努力都不难。然后可以处理数据,但您可以选择。