【问题标题】:PHP - Get multiple email attachmentsPHP - 获取多个电子邮件附件
【发布时间】:2015-05-27 18:29:11
【问题描述】:

我能够修改下面的代码,以便将电子邮件附件保存在特定文件夹中并获取附件链接并下载它。 然而到目前为止一切正常,如果一封电子邮件有多个附件,脚本会输出(回显)第一个附件名称和链接,但会将它们全部保存在服务器文件夹中。

示例:(一个附件 - 工作正常)

附件:attachment_1.jpg

下载链接:下载


示例:(多个附件)

附件:附件_1.jpg、附件_2.jpg等。

下载链接:下载1、下载2等。


如何打印电子邮件的所有附件名称?

echo 'Attachment:' . ' ' . $name[0] . '</br>'; ???

参见下面的代码。

    $structure = imap_fetchstructure($imap, $m);

    $attachments = array();
    if (isset($structure->parts) && count($structure->parts)) {

        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($imap, $m, $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']);
                }
            }
        }
    }

    foreach ($attachments as $key => $attachment) {
        $name = $attachment['name'];
        $contents = $attachment['attachment'];
        file_put_contents(STYLESHEETPATH . '/email_attachments/' . $name, $contents);
    }

    $download_link = get_stylesheet_directory_uri() . '/email_attachments/' . $name;

    //imap_setflag_full($imap, $i, "\\Seen");
    //imap_mail_move($imap, $i, 'Trash');
    //print_r($attachment);
    echo '</br>From:' . ' ' . $from_email . '</br>';
    echo 'To:' . ' ' . $to . '</br>';

    if($name){
        echo 'Attachment:' . ' ' . $name . '</br>';
        echo 'Path Saved: ' . ' ' . STYLESHEETPATH . '/email_attachments/' . $name . '</br>';
        echo 'Download Link: ' . ' ' . '<a href="' . $download_link . '"> Download</a>' . '</br>';
    }

    echo 'Subject:' . ' ' . $subject . '</br></br>';
}

【问题讨论】:

  • 您需要添加一个 foreach 来遍历所有附件名称。
  • 这就是我艰难的悬停,不知道在哪里添加它。我试过 echo 'Attachment:' 。 ' ' 。 $名称。 '';但什么都不做

标签: php email email-attachments


【解决方案1】:

这里

if($name){
    echo 'Attachment:' . ' ' . $name . '</br>';
    echo 'Path Saved: ' . ' ' . STYLESHEETPATH . '/email_attachments/' .$name . '</br>';
    echo 'Download Link: ' . ' ' . '<a href="' . $download_link . '"> Download</a>' . '</br>';
}

您只打印一个附件(foreach 迭代中留下的最后一个附件),您需要将其包装在 foreach 中

foreach ($attachments as $key => $attachment) {
    $name = $attachment['name'];
    $contents = $attachment['attachment'];
    // Put your code to print here.
}

【讨论】:

    猜你喜欢
    • 2012-08-23
    • 1970-01-01
    • 2017-10-14
    • 2010-09-12
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多