如何使用api从gmail下载附件

时间:2011-11-17 13:05:22

标签: php cakephp gmail imap

我已尝试过这里提到的IMAP解决方案。

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

我感谢任何帮助。

感谢。

2 个答案:

答案 0 :(得分:1)

我将把你推荐给Apache Zeta Components的Mail组件。我前几天只是玩弄它,从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("user@gmail.com","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 :(得分: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;