作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我在用 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);
最佳答案
您的代码有很多问题:
CRLF
结尾。你只有 LF
。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 "--"
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/
今天小米米家官方微博宣布,小米手表尊享版开启限量定金预售,3 月 3 日早 10 点起支付尾款。购买即赠 399 元小米真无线蓝牙耳机 Air 2。 小米手表尊享版相比普通版核心没有变化,采用
今天下午,小米官方公布了小爱触屏音箱Pro 8,可以看到触控屏幕尺寸比较大,音箱上“长”了一个平板。据悉,小米小爱触屏音箱Pro 8具有白色的配色设计,下方有一个长条状的扬声器,上方带有一个尺寸比较
我是一名优秀的程序员,十分优秀!