12

このメールから添付ファイルを取得するにはどうすればよいですか?

この電子メールはアップルコンピュータから送信され、電子メールの構造は他のどのようなものでもありません(驚き)。ここでは、配置のある部分が他の部分よりも1次元深くなっています。

スクリプトは、ファイルの一部が最初の次元にある他のすべての電子メールで機能しますが、この電子メールでは機能しません

$part->dparameters[0]->valueファイル名を返しますがstrlen($data)0

imapストリーム

$structure = imap_fetchstructure($this->stream, $this->msgno);

if(isset($structure->parts)){
    print_r($structure->parts);
    $this->parse_parts($structure->parts);
}

function parse_parts($parts){
    foreach($parts as $section => $part){
        if(isset($part->parts)){

            // some mails have one extra dimension
            $this->parse_parts($part->parts);

        }
        elseif(isset($part->disposition)){
            if(in_array(strtolower($part->disposition), array('attachment','inline'))){
                $data = imap_fetchbody($this->stream, $this->msgno, $section+1);
                echo $part->dparameters[0]->value.' '.strlen($data)."\n";
            }
        }
    }
}

print_r

Array
(
    [0] => stdClass Object
        (
            [type] => 0
            [encoding] => 0
            [ifsubtype] => 1
            [subtype] => PLAIN
            [ifdescription] => 0
            [ifid] => 0
            [lines] => 15
            [bytes] => 173
            [ifdisposition] => 0
            [ifdparameters] => 0
            [ifparameters] => 1
            [parameters] => Array
                (
                    [0] => stdClass Object
                        (
                            [attribute] => CHARSET
                            [value] => us-ascii
                        )

                )

        )

    [1] => stdClass Object
        (
            [type] => 1
            [encoding] => 0
            [ifsubtype] => 1
            [subtype] => MIXED
            [ifdescription] => 0
            [ifid] => 0
            [bytes] => 23420
            [ifdisposition] => 0
            [ifdparameters] => 0
            [ifparameters] => 1
            [parameters] => Array
                (
                    [0] => stdClass Object
                        (
                            [attribute] => BOUNDARY
                            [value] => Apple-Mail=_800896E0-A9C9-456E-B063-79CED9DD4FD7
                        )

                )

            [parts] => Array
                (
                    [0] => stdClass Object
                        (
                            [type] => 0
                            [encoding] => 0
                            [ifsubtype] => 1
                            [subtype] => HTML
                            [ifdescription] => 0
                            [ifid] => 0
                            [bytes] => 136
                            [ifdisposition] => 0
                            [ifdparameters] => 0
                            [ifparameters] => 1
                            [parameters] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [attribute] => CHARSET
                                            [value] => us-ascii
                                        )

                                )

                        )

                    [1] => stdClass Object
                        (
                            [type] => 3
                            [encoding] => 3
                            [ifsubtype] => 1
                            [subtype] => PDF
                            [ifdescription] => 0
                            [ifid] => 0
                            [bytes] => 17780
                            [ifdisposition] => 1
                            [disposition] => INLINE
                            [ifdparameters] => 1
                            [dparameters] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [attribute] => FILENAME
                                            [value] => 057 - LPJ - Stik og labels.pdf
                                        )

                                )

                            [ifparameters] => 1
                            [parameters] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [attribute] => NAME
                                            [value] => 057 - LPJ - Stik og labels.pdf
                                        )

                                )

                        )

                    [2] => stdClass Object
                        (
                            [type] => 0
                            [encoding] => 4
                            [ifsubtype] => 1
                            [subtype] => HTML
                            [ifdescription] => 0
                            [ifid] => 0
                            [lines] => 75
                            [bytes] => 4931
                            [ifdisposition] => 0
                            [ifdparameters] => 0
                            [ifparameters] => 1
                            [parameters] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [attribute] => CHARSET
                                            [value] => us-ascii
                                        )

                                )

                        )

                )

        )

)
4

4 に答える 4

16

ネストされた添付ファイルに正しいセクション番号を指定していません。再帰ステップでセクション番号を渡す必要があります。

function parse_parts($parts, $parentsection = ""){
    foreach($parts as $subsection => $part){
        $section = $parentsection . ($subsection + 1);
        if(isset($part->parts)){

            // some mails have one extra dimension
            $this->parse_parts($part->parts, $section . "." );

        }
        elseif(isset($part->disposition)){
            if(in_array(strtolower($part->disposition), array('attachment','inline'))){
                $data = imap_fetchbody($this->stream, $this->msgno, $section );
                echo 'Getting section ' . $section;
                echo $part->dparameters[0]->value.' '.strlen($data)."\n";
            }
        }
    }
}

(テストされていませんが、正しいことを行う必要があります...)

于 2012-07-10T01:51:32.593 に答える
1

Mac のメールについては特に知りませんが、各部分を通過して「添付ファイル」であるかどうかを確認し、後で呼び出すことができるようにファイルをどこかに保存するコードを次に示します。

$body = imap_fetchstructure($box, $i);
$attachments = '';
$att = count($body->parts);
if($att >=2) {
for($a=0;$a<$att;$a++) {
if($body->parts[$a]->disposition == 'ATTACHMENT') {
$file = imap_base64(imap_fetchbody($box, $i, $a+1));
$string = genRandomString();
    if(!file_exists('/var/www/email_store/'.$_SESSION['site_user_id'])) {
        mkdir('/var/www/email_store/'.$_SESSION['site_user_id'].'/');   
    }
    $attachments .= $body->parts[$a]->dparameters[0]->value.'[#]'.$string.',';
    file_put_contents('/var/www/email_store/'.$_SESSION['site_user_id'].'/'.$string,$file);
    }
}       
于 2012-07-06T14:14:01.623 に答える