PHP imap下载附件 [英] PHP imap download attachment

查看:189
本文介绍了PHP imap下载附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面,我使用PHP的imap库遍历电子邮件. 它可以正常工作,但是我一直坚持从邮件中保存附件.

Below I am looping through emails, using PHPs imap libraries. It works, but I'm stuck on saving attachments from the message.

<?php

$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'someemail@somedomain.com';
$password = 'somepassword';

/* try to connect */
$imap_connection = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

/* grab emails */
$emails = imap_search($imap_connection,'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) {

        $structure = imap_fetchstructure($imap_connection,$email_number);
        if(isset($structure->parts) && count($structure->parts)){

            for($i = 0; $i < count($structure->parts); $i++){   


                if(!empty($structure->parts[$i]->bytes)){

                    if(!empty($ifdparameters)){
                        echo "File: ----".$structure->parts[$i]->dparameters[0]->value; 
                                            // returns: testMeOut.jpg

                    }
                }


            } //eof $i loop


        } //eof $structure->parts 


          } // eof foreach $emails

} // eof if($emails)

?>

我正在尝试了解如何将附件保存到服务器上的目录. 我知道:$ structure-> parts [$ i]-> dparameters [0]-> value;返回附件的名称.

I am trying to understand how to save an attachment to a directory on my server. I know: $structure->parts[$i]->dparameters[0]->value; is returning the name of the attachment.

我确定imap_fetchbody()需要做些事情,但是我对在线阅读教程和php网站感到非常困惑.

I'm sure there is something I need to do with imap_fetchbody(), but I'm absolutely confused reading the tutorials online and php's site.

有人能看到我遗失的最后几步,有关如何从$ message中保存附件吗?

Can anyone see my missing last few steps on how to save an attachment from the $message?

推荐答案

一旦编写该文件即可下载Excel文件;

Once written that for downloading Excel files;

猜猜它可以更改为几乎可以执行任何电子邮件附件操作.

Guess it can be changed to do almost anything email attachment-wise.

基本上,它演示了您缺少的两个步骤:

Basically it demonstrates the two steps that you are missing:

    $message = array();
    $message["attachment"]["type"][0] = "text";
    $message["attachment"]["type"][1] = "multipart";
    $message["attachment"]["type"][2] = "message";
    $message["attachment"]["type"][3] = "application";
    $message["attachment"]["type"][4] = "audio";
    $message["attachment"]["type"][5] = "image";
    $message["attachment"]["type"][6] = "video";
    $message["attachment"]["type"][7] = "other";

    function get_decode_value($message, $encoding) {
        switch($encoding) {
            case 0:case 1:$message = imap_8bit($message);break;
            case 2:$message = imap_binary($message);break;
            case 3:case 5:$message=imap_base64($message);break;
            case 4:$message = imap_qprint($message);break;
        }
        return $message;
    }

    for ($jk = 1; $jk <= imap_num_msg($mbox); $jk++) {

        $structure = imap_fetchstructure($mbox, $jk , FT_UID);

        /* retrieve message timestamp */
        $header = imap_headerinfo($mbox, $jk);
        $timestamp = $header->udate;

        $parts = $structure->parts;
        $fpos=2;
        for($i = 1; $i < count($parts); $i++) {
            $message['pid'][$i] = ($i);
            $part = $parts[$i];

            if($part->disposition == "ATTACHMENT") {

                $message["type"][$i] = $message["attachment"]["type"][$part->type] . "/" . strtolower($part->subtype);
                $message["subtype"][$i] = strtolower($part->subtype);

                $ext = $part->subtype;
                $params = $part->dparameters;
                $filename = $part->dparameters[0]->value;
                $body="";$data="";

                if($filename=='SomeFile.XLS'){
                    $body = imap_fetchbody($mbox, $jk, $fpos);
                    $dst = '/SomeLocalPath/SomeOtherFile.xls';

                    /* don't overwrite */
                    if(!file_exists($dst)){
                        $fp = fopen($dst, 'w');
                        $data = get_decode_value($body, $part->type);
                        fputs($fp, $data);
                        fclose($fp);
                    }
                }
                $fpos++;
            }
        }
    }

我想知道为什么无法通过任何Google API访问附件...

I'd rather wonder why the attachment can't be accessed via any Google API ...

为什么没有双向GMail API,如单向Mandrill API?

Why there's no bi-directional GMail API, like the uni-directional Mandrill API?

这篇关于PHP imap下载附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆