主题或消息中带有特殊字符的电子邮件的 PHP 邮件标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15250140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Mail header for emails with special characters in the subject or message
提问by Laurent
My code:
我的代码:
$to = '[email protected]';
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
$header = "From: [email protected]\r\n";
$header.= "MIME-Version: 1.0\r\n";
$header.= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$header.= "X-Priority: 1\r\n";
mail($to, $subject, $message, $header);
When i send a mail with special characters such as ?e-??-?“?,??e??,
in the message, it works but spacing is no longer dealt with (every new line or space gets removed)
And the second problem is that the special characters are not displayed in the subject.
They just output like: ?ʼaʼ
当我发送带有特殊字符的邮件时,例如?e-??-?“?,??e??, 在消息中,它可以工作但不再处理间距(每个新行或空格都被删除)第二个问题是特殊字符未显示在主题中。他们只是输出如下:?ʼaʼ
Thanks in advance!
提前致谢!
回答by TFennis
Content-Type: text/html
内容类型:文本/html
If you set this header that means you have to send HTML to the user. You can either decide to use something like TinyMCE to let the user write the message in a Word-style editor and use the HTML output from that. Or set your headers to plaintext.
如果你设置了这个标题,就意味着你必须向用户发送 HTML。您可以决定使用类似 TinyMCE 之类的东西,让用户在 Word 样式的编辑器中编写消息并使用其中的 HTML 输出。或者将您的标题设置为纯文本。
Content-Type: text/plain
内容类型:文本/纯文本
EDIT: Try this
编辑:试试这个
$to = '[email protected]';
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
$header = "From: [email protected]\r\n";
$header.= "MIME-Version: 1.0\r\n";
$header.= "Content-Type: text/plain; charset=utf-8\r\n";
$header.= "X-Priority: 1\r\n";
mail($to, $subject, $message, $header);
回答by user3820294
I have used this header and this is worked for me...
我已经使用了这个标题,这对我有用......
$headers = '';
$headers = 'MIME-Version: 1.0'.PHP_EOL;
$headers .= 'Content-type: text/html; charset=iso-8859-1'.PHP_EOL;
$headers .= 'From: [email protected]<From: [email protected]>'.PHP_EOL;
回答by Ghigo
Don't use mail() function. Use a fully crafted class that does (correctly) the job for you.
不要使用 mail() 函数。使用完全精心设计的类来(正确地)为您完成工作。
回答by Babblo
the problem with your code is because special characters need "special" charset too.
您的代码的问题是因为特殊字符也需要“特殊”字符集。
Try changing the charset=ISO to charset=UTF8...
尝试将 charset=ISO 更改为 charset=UTF8 ...
Also, PHP mail() functions works ok, but you will find a lot more benefits and options if you go for a better solution like Swift Mailer
此外,PHP mail() 函数可以正常工作,但是如果您寻求更好的解决方案,例如Swift Mailer,您会发现更多好处和选项

