本文介绍了发送带有utf-8文件名的MIME编码的电子邮件附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的人们,

最近3天,我在网上搜索了一个答案,但找不到任何答案.
我发现了很多差不多"的案例,但没有一个正是我想要的.

I spent the last 3 days searching the web for an answer and I couldn't find any.
I found plenty of "almost" cases but none was exactly what I was looking for.

我可以在希伯来语中获取主题和正文消息,但无法在希伯来语中获取附件的文件名.

I am able to get the subject and the body message in Hebrew, but I can't get the attached file name in Hebrew.

顺便说一句,我对PHPMailer ect等第三方程序不感兴趣.

Btw, I'm not interested in third party programs like PHPMailer ect.

这就是我得到的:

这就是我想要得到的:

这是我的代码,非常简单.

Here is my code, very simple..

$boundary = uniqid("HTMLEMAIL");
$separator = md5(time());
$eol = PHP_EOL;

// attachment name
$fileName = "שלום.pdf";
var_dump($fileName);

$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));

// main header (multipart mandatory)
$headers = [];
$headers[] = "From: $from";
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
$headers[] = "This is a MIME encoded message.";

// message
$msg = "--".$separator.$eol;
$msg .= "Content-Type: text/html; charset=UTF-8".$eol;
$msg .= "Content-Transfer-Encoding: base64".$eol.$eol;
$msg .= chunk_split(base64_encode($message)).$eol.$eol;

// attachment
$msg .= "--".$separator.$eol;
$msg .= "Content-Type: application/pdf; name=\"".$fileName."\"".$eol;
$msg .= "Content-Transfer-Encoding: base64".$eol.$eol;
$msg .= "Content-Disposition: attachment".$eol;
$msg .= $attachment.$eol;
$msg .= "--".$separator."--";

mail($to,'=?UTF-8?B?'.base64_encode($subject).'?=', $msg, implode("\n\r", $headers));

推荐答案

根据RFC2047,您不能在Content-Type标头的参数中使用ascii以外的编码.

According to RFC2047 you can't have encodings other than ascii in parameters of Content-Type header.

根据RFC2231,您可以尝试定义扩展参数: Content-Type:应用程序/pdf;名称* = utf-8''%D7%A9%D7%9C%D7%95%D7%9D%2E%70%64%66

According to RFC2231 you can try to define extended parameter:Content-Type: application/pdf; name*=utf-8''%D7%A9%D7%9C%D7%95%D7%9D%2E%70%64%66

我不知道它的支持程度.

I have no idea how well it is supported.

为此我无法提出oneliner,但是您可以尝试改编

I can't come up with oneliner for that, but you can try to adapt this one PHP convert string to hex and hex to string

根据评论更新:
尽管规范明确禁止这样做,但大多数邮件客户端应理解以下格式'name =?UTF-8?B?'.base64_encode($ filename).'?='

我建议您出于理智的目的使用它.

I suggest you to use it for sanity sake.

这篇关于发送带有utf-8文件名的MIME编码的电子邮件附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 02:50