如何使用api从gmail下载附件

发布于 2024-12-15 21:21:32 字数 1607 浏览 0 评论 0原文

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

从 php 下载 gmail 附件

我正在使用 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);

我很感激任何帮助。

谢谢。

I have tried IMAP solution mentioned here.

download gmail attachements from php

I am using cakephp. It times out after 60 seconds.

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

I appreciate any help.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明天过后 2024-12-22 21:21:32

我将向您推荐 Apache Zeta 的 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("[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 ) );
        }
    }
}

I'm going to refer you to Mail component from Apache Zeta Components. I waas just toying with it the other day and both sending and retrieving attachments from Gmail was really easy (just follow tutorials)

AN example code:

<?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 ) );
        }
    }
}
笑梦风尘 2024-12-22 21:21:32

你可以试试我的 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;

You could try my ImapLib.
Worked for me with pdf attachments:
https://github.com/dereuromark/tools/blob/2.0/Lib/ImapLib.php

basically I use it like this inside my model:

    $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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文