从 php 下载 gmail 附件

发布于 2024-09-12 18:49:27 字数 40 浏览 5 评论 0原文

您能告诉我如何使用 php 从 gmail 帐户下载附件吗? 谢谢

can you please tell , how to download attachments from gmail account using php ?
thanks

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

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

发布评论

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

评论(3

記憶穿過時間隧道 2024-09-19 18:49:27

您可以使用 php pop 或 imap 客户端获取邮件并从中提取附件

you could use a php pop or imap client and fetch the mail and extract the attachments from that

や莫失莫忘 2024-09-19 18:49:27

正如其他回答者所述,建议使用 IMAP 解决方案,可以在 http://davidwalsh.name/ 找到gmail-php-imap。确保检查您的防火墙,并在 PHP 安装中启用 OpenSSL 和 IMAP。

http://www.electrictoolbox.com/extract-attachments-email-php- imap/ 有关于如何使用 imap_fetchstruction 分析每封电子邮件的结构的优秀代码参考。每条消息的结构中都包含附件。用户在 http://www.php.net/manual/ 贡献了注释en/function.imap-fetchstruct.php 特别有用(正如文档一样)。

基本上,您必须

  • 循环遍历结构部分,
  • 确定其中哪些是
  • 适当的附件解码。

不同的电子邮件客户端的结构会略有不同,但只要稍加努力,就不会太难。然后可以按照您选择的方式处理数据。

As stated by other answerers, an IMAP solution is recommended, and can be found at http://davidwalsh.name/gmail-php-imap. Make sure to check your firewall, and enable the OpenSSL and IMAP in your PHP installation.

http://www.electrictoolbox.com/extract-attachments-email-php-imap/ has excellent code references regarding how to analyze each email's structure using imap_fetchstructure. In the structure of each message lie the attachments. The user contributed notes at http://www.php.net/manual/en/function.imap-fetchstructure.php are particularly helpful (as is the documentation).

Basically, you have to

  • loop through the structure parts
  • determine which of them are attachments
  • decode as appropriate.

Different email clients will have slightly different structures, but it's not too hard with a little effort. The data can then be handled however you choose.

逆流 2024-09-19 18:49:27

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

IMAP Solution

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

You must add to it attachment control and you can read this because from it is this source code http://davidwalsh.name/gmail-php-imap

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