带附件的 PHP 邮件 - 额外文件:第 1.4 部分

发布于 2024-08-20 15:53:02 字数 1269 浏览 1 评论 0原文

我使用以下代码发送带有附件的电子邮件:

    $mime_boundary = "<<<--==+X[".md5(time())."]";

    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: multipart/mixed;\r\n";
    $headers .= " boundary=\"".$mime_boundary."\"";

    $message .= "This is a multi-part message in MIME format.\r\n\r\n";
    $message .= "--".$mime_boundary."\r\n";

    $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
    $message .= "Content-Transfer-Encoding: 7bit\r\n";
    $message .= "\r\n";
    $message .= "$message_body\r\n";
    $message .= "--".$mime_boundary."\r\n";

    foreach($attachments as $filename => $data)
    {
        $message .= "Content-Type: application/octet-stream;\r\n";
        $message .= " name=\"$filename\"\r\n";
        $message .= "Content-Transfer-Encoding: quoted-printable\r\n";
        $message .= "Content-Disposition: attachment;\r\n";
        $message .= " filename=\"$filename\"\r\n";
        $message .= "\r\n";
        $message .= chunk_split(base64_encode($data));
        $message .= "\r\n";
        $message .= "--".$mime_boundary."\r\n";
    }

    mail($email_address, $email_subject, $message, $headers);

效果很好,只是还附加了一个额外的文件(称为“第 1.4 部分”)。

有没有办法不添加这个?

干杯, 担。

I'm using the following code to send an email with attachments:

    $mime_boundary = "<<<--==+X[".md5(time())."]";

    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: multipart/mixed;\r\n";
    $headers .= " boundary=\"".$mime_boundary."\"";

    $message .= "This is a multi-part message in MIME format.\r\n\r\n";
    $message .= "--".$mime_boundary."\r\n";

    $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
    $message .= "Content-Transfer-Encoding: 7bit\r\n";
    $message .= "\r\n";
    $message .= "$message_body\r\n";
    $message .= "--".$mime_boundary."\r\n";

    foreach($attachments as $filename => $data)
    {
        $message .= "Content-Type: application/octet-stream;\r\n";
        $message .= " name=\"$filename\"\r\n";
        $message .= "Content-Transfer-Encoding: quoted-printable\r\n";
        $message .= "Content-Disposition: attachment;\r\n";
        $message .= " filename=\"$filename\"\r\n";
        $message .= "\r\n";
        $message .= chunk_split(base64_encode($data));
        $message .= "\r\n";
        $message .= "--".$mime_boundary."\r\n";
    }

    mail($email_address, $email_subject, $message, $headers);

Which works fine, except that an extra file is also attached (called "Part 1.4").

Is there a way to not have this added?

Cheers,
Dan.

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

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

发布评论

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

评论(1

请止步禁区 2024-08-27 15:53:02

IIRC 最后一部分分隔符必须是 --something unique--,即在您的情况下

$message .= "--".$mime_boundary."--\r\n";

但 mime 邮件或多或少是一个已解决的问题(即对于应用程序开发人员来说,正确完成时会很无聊,而正确完成时会很烦人)做错了;-))。帮自己一个忙,使用 Swiftmailer 或任何其他下降邮件库/类之类的东西。

IIRC the last part separator must be --something unique--, i.e. in your case

$message .= "--".$mime_boundary."--\r\n";

But mime mail is more or less a solved problem ( i.e. for an application developer it's boring when done correctly and reeeeally annoying when done wrong ;-) ). Do yourself a favor and use something like Swiftmailer or any other descend mailing library/class.

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