来自PHP的电子邮件已经破坏了Subject标题编码 [英] Email from PHP has broken Subject header encoding
问题描述
我的PHP脚本向用户发送电子邮件,当电子邮件到达邮箱时,主题行( $ subject
)的字符包含 a ^ £
添加到我的主题文本的末尾。这显然是编码问题。电子邮件内容本身很好,只是主题行被破坏。
我已经搜索过,但找不到如何正确编码我的主题。
这是我的标题。请注意,我使用内容类型
与 charset = utf-8
和 Content-转移编码:8bit
。
//设置所有必需的标题
$ headers = From:$ sender_name< $ from> \\\
;
$ headers。=Reply-To:$ sender_name< $ from> \\\
;
$ headers。=X-Sender:$ sender_name< $ from> \\\
;
$ headers。=X-Mailer:PHP4\\\
; // mailer
$ headers。=X-Priority:3\\\
; // 1 UrgentMessage,3 Normal
$ headers。=MIME-Version:1.0\\\
;
$ headers。=X-MSMail-Priority:High\\\
;
$ headers。=重要性:3\\\
;
$ headers。=Date:$ date\\\
;
$ headers。=Delivered-to:$ to\\\
;
$ headers。=返回路径:$ sender_name< $ from> \\\
;
$ headers。=Envelope-from:$ sender_name< $ from> \\\
;
$ headers。=Content-Transfer-Encoding:8bit\\\
;
$ headers。=Content-Type:text / plain; charset = UTF-8\\\
;
更新实际和最新的答案,请查看 Palec的答案。
Content-Type 中指定的字符编码仅描述消息正文的字符编码,但不描述头文件。您需要使用编码字语法使用 可引用编码或 Base64 encoding :
encoded-word ==?字符集?编码?编码文本?=
您可以使用 imap_8bit
for quoted-printable encoding and base64_encode
主题:=?UTF-8?B?base64_encode .imap_8bit($ subject)?=
My PHP script sends email to users and when the email arrives to their mailboxes, the subject line ($subject
) has characters like a^£
added to the end of my subject text. This is obviously and encoding problem. The email message content itself is fine, just the subject line is broken.
I have searched all over but can’t find how to encode my subject properly.
This is my header. Notice that I’m using Content-Type
with charset=utf-8
and Content-Transfer-Encoding: 8bit
.
//set all necessary headers
$headers = "From: $sender_name<$from>\n";
$headers .= "Reply-To: $sender_name<$from>\n";
$headers .= "X-Sender: $sender_name<$from>\n";
$headers .= "X-Mailer: PHP4\n"; //mailer
$headers .= "X-Priority: 3\n"; //1 UrgentMessage, 3 Normal
$headers .= "MIME-Version: 1.0\n";
$headers .= "X-MSMail-Priority: High\n";
$headers .= "Importance: 3\n";
$headers .= "Date: $date\n";
$headers .= "Delivered-to: $to\n";
$headers .= "Return-Path: $sender_name<$from>\n";
$headers .= "Envelope-from: $sender_name<$from>\n";
$headers .= "Content-Transfer-Encoding: 8bit\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\n";
Update For a more practical and up-to-date answer, have a look at Palec’s answer.
The specified character encoding in Content-Type does only describe the character encoding of the message body but not the header. You need to use the encoded-word syntax with either the quoted-printable encoding or the Base64 encoding:
encoded-word = "=?" charset "?" encoding "?" encoded-text "?="
You can use imap_8bit
for the quoted-printable encoding and base64_encode
for the Base64 encoding:
"Subject: =?UTF-8?B?".base64_encode($subject)."?="
"Subject: =?UTF-8?Q?".imap_8bit($subject)."?="
这篇关于来自PHP的电子邮件已经破坏了Subject标题编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!