Как скачать вложения из Gmail с помощью API - PullRequest
0 голосов
/ 17 ноября 2011

Я пробовал упомянутое здесь решение IMAP.

загрузка вложений gmail с 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 ]

1 голос
/ 17 ноября 2011

Вы можете попробовать мой 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;
1 голос
/ 17 ноября 2011

Я собираюсь отослать вас к Mail компоненту от Apache Zeta Components.Я просто играл с ним на днях, и отправка и получение вложений из 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 ) );
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...