php - 尽管在 header 中发送了 base64,但仍无法识别附件

标签 php email mime

我在用 php 邮件发送附件时遇到问题。问题是电子邮件似乎已发送并已收到,但未被识别为附件。如果我进入邮件的来源,我可以看到它正在发送 base64 并包含附件名称,所以我很困惑为什么客户端无法识别它。有什么想法吗?

$uid = md5(uniqid(time()));
$headers = "From: ".$from_name." <".$mailto.">\n";
$headers .= "Reply-To: ".$from_mail."\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= "--".$uid."\n";
$headers .= "Content-type:text/plain\n";
$headers .= "Content-Transfer-Encoding: 8bit\n";
$headers .= $message."\n";

foreach ($_FILES as $file) {
    $name = $file['name'];
    $handle = fopen($file['tmp_name'], "r");
    $content = fread($handle, $file['size']);
    fclose($handle);
    $content = chunk_split(base64_encode($content));

    $headers .= "--{$uid}\n";
    $headers .= "Content-Type: {$file['type']}; name=\"{$name}\"\n";
    $headers .= "Content-Transfer-Encoding: base64\n";
    $headers .= "Content-Disposition: attachment; filename=\"{$name}\"\n";
    $headers .= $content;
}

$headers .= "--{$uid}--";
$mailto = 'myemail@email.com';
return mail($mailto, $subject, $message, $headers, '-f' . $mailto);

最佳答案

问题

您的代码有很多问题:

  • 电子邮件 header 必须以 CRLF 结尾。你只有 LF
  • B64 编码的文件和消息被添加到标题中。它们应作为邮件正文发送。
  • 每部分的消息头和消息体之间必须有一个额外的CRLF
  • 正文部分必须由以 CRLF 开头的分隔符分隔。

电子邮件语法

这是根据 RFC 2046 的多部分消息体结构的摘录。请特别注意 CRLF。 (BNF 语法,有些简化。)


multipart-body := [preamble CRLF]
                  dash-boundary CRLF
                  body-part *encapsulation
                  close-delimiter
                  [CRLF epilogue]

dash-boundary := "--" boundary

body-part := MIME-part-headers [CRLF *OCTET]

encapsulation := delimiter
                 CRLF body-part

delimiter := CRLF dash-boundary

close-delimiter := delimiter "--"

The code

Your code could look something like this:

$uid = md5(uniqid(time()));
$headers = "From: ".$from_name." <".$mailto.">\r\n";
$headers .= "Reply-To: ".$from_mail."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n";

$body = "This is a multi-part message in MIME format.\n";
$body .= "--".$uid."\r\n";
$body .= "Content-type:text/plain\r\n";
$body .= "Content-Transfer-Encoding: 8bit\r\n";
$body .= "\r\n";
$body .= $message;

foreach ($_FILES as $file) {
  $name = $file['name'];
  $handle = fopen($file['tmp_name'], "r");
  $content = fread($handle, $file['size']);
  fclose($handle);
  $content = chunk_split(base64_encode($content));

  $body .= "\r\n--{$uid}\r\n";
  $body .= "Content-Type: {$file['type']}; name=\"{$name}\"\r\n";
  $body .= "Content-Transfer-Encoding: base64\r\n";
  $body .= "Content-Disposition: attachment; filename=\"{$name}\"\r\n";
  $body .= "\r\n";
  $body .= $content;
}

$body .= "\r\n--{$uid}--";
$mailto = 'myemail@email.example';
return mail($mailto, $subject, $body, $headers, '-f' . $mailto);

引用资料

关于php - 尽管在 header 中发送了 base64,但仍无法识别附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23340291/

相关文章:

php - 我如何对 Web 应用程序中的所有输出进行 HTML 编码?

php - 用 Composer 安装 Twig 给出错误

email - 在容器中为您的应用程序设置 SMTP 服务器

javascript - 如何构建多部分 MIME 请求并使用 AngularJS 的 $http 方法发布它?

http - 请求被拒绝,因为在 Grails 中没有找到多部分边界

javascript - 如何在 javascript 中使用自动递增 id?

php - 使用 Android Studio 和 PHP 的 Android 到 Wamp 服务器连接

php - 如何在 Symfony 中向电子邮件添加附件?

html - 需要在发起http响应后通知邮件发送状态

python - Django 管理员突然仅在生产环境中注销(拒绝执行脚本,因为其 MIME 类型 ('text/html' )不可执行)