使用imap php附件的大小为0

时间:2018-10-04 02:59:29

标签: php imap

我可以在邮件的所有附件的finalpath目录上“创建文件”,但是所有文件的大小均为零,例如空附件。

在该社区中发现了大多数代码。

我做错了什么?

谢谢

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

    $attachments[$i] = array(
        'is_attachment' => false,
        'filename' => '',
        'name' => '',
        'attachment' => ''
    );

    if($structure->parts[$i]->ifdparameters) {
        foreach($structure->parts[$i]->dparameters as $object) {
            if(strtolower($object->attribute) == 'filename') {
                $attachments[$i]['is_attachment'] = true;
                $attachments[$i]['filename'] = $object->value;
            }
        }
    }

    if($structure->parts[$i]->ifparameters) {
        foreach($structure->parts[$i]->parameters as $object) {
            if(strtolower($object->attribute) == 'name') {
                $attachments[$i]['is_attachment'] = true;
                $attachments[$i]['name'] = $object->value;
            }
        }
    }

    if($attachments[$i]['is_attachment']) {
        $attachments[$i]['attachment'] = imap_fetchbody($connection, $message_number, $i+1);
        if($structure->parts[$i]->encoding == 3) { // 3 = BASE64
            $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
        }
        elseif($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
            $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
        }
    }           }
            // GUARDAMOS LOS ADJUNTOS

 /* iterate through each attachment and save it */
  foreach($attachments as $attachment){
    if($attachment['is_attachment'] == 1)
    {
        $filename = $attachment['name'];
        if(empty($filename)) $filename = $attachment['filename'];

        if(empty($filename)) $filename = time() . ".dat";
        $fp = fopen("./finalpath/".$filename, "w+");
        fwrite($fp, $attachment['attachment']);
        fclose($fp);
    }
}


}
}

非常感谢您的帮助。

0 个答案:

没有答案