2011-11-17 76 views
0

我已經嘗試過這裏提到的IMAP解決方案。如何使用api從Gmail下載附件

download gmail attachements from php

我使用CakePHP。它在60秒後超時。

/* connect to gmail */ 
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX'; 
$username = 'david***@gmail.com'; 
$password = '*********'; 

/* 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); 

我很感激任何幫助。

謝謝。

+0

您需要發佈你試過代碼或提供更多的細節關於你的問題。 – nickb

回答

1

我打算從Apache Zeta Components轉到Mail組件。我WAAS只是玩弄它的一天,從Gmail中發送和檢索的附件是很容易(只要按照教程)

一個例子代碼:

<?php 

error_reporting(E_ALL); 

require_once('classes/zeta/Base/src/ezc_bootstrap.php'); 

spl_autoload_register(array('ezcBase', 'autoload')); 

$options = new ezcMailImapTransportOptions(); 
$options->ssl = true; 

$imap = new ezcMailImapTransport("imap.gmail.com",993,$options); 

$imap->authenticate("[email protected]","password"); 
$mailboxes = $imap->listMailboxes(); 

$imap->selectMailbox('[Gmail]/All Mail'); 

$set = $imap->fetchAll(); 

$parser = new ezcMailParser(); 

$mail = $parser->parseMail($set); 

for ($i = 0; $i < count($mail); $i++) 
{ 

    // Process $mail[$i] such as use $mail[$i]->subject, $mail[$i]->body 
    echo "From: {$mail[$i]->from}, Subject: {$mail[$i]->subject}\n"; 
    // Save the attachments to another folder 
    $parts = $mail[$i]->fetchParts(); 
    foreach ($parts as $part) 
    { 
     if ($part instanceof ezcMailFile) 
     { 
      rename($part->fileName, __DIR__ . '/' . basename($part->contentDisposition->displayFileName)); 
     } 
    } 
} 
1

你可以試試我的ImapLib。 爲我工作與PDF附件: https://github.com/dereuromark/tools/blob/2.0/Lib/ImapLib.php

基本上我用它像這樣我的模型裏面:

$code = 'privat'; 
    $account = Configure::read('Mailbox.'.$code); 
    if (!$account) { 
     trigger_error('Credentials missing for '.$code); 
     return array(); 
    } 

    App::import('Lib', 'Tools.ImapLib'); 
    $Imap = new ImapLib(); 
    $Imap->set('service', 'pop3'); 
    $res = $Imap->connect($account['address'], $account['password'], Configure::read('Mailbox.host')); 
    if (!$res) { 
     return array(); 
    } 
    $messages = $Imap->msgList(); 
    return $messages; 
相關問題