gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-10-31 23:31:02 26 4
gpt4 key购买 nike

我在用 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 := "--" boundarybody-part := MIME-part-headers [CRLF *OCTET]encapsulation := delimiter                 CRLF body-partdelimiter := CRLF dash-boundaryclose-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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com