問(wèn)題描述
我的 PHP 腳本向用戶(hù)發(fā)送電子郵件,當(dāng)電子郵件到達(dá)他們的郵箱時(shí),主題行 ($subject
) 的末尾添加了類(lèi)似 a^£
的字符我的主題文本.這顯然是編碼問(wèn)題.電子郵件內(nèi)容本身沒(méi)問(wèn)題,只是主題行壞了.
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.
這是我的標(biāo)題.請(qǐng)注意,我將 Content-Type
與 charset=utf-8
和 Content-Transfer-Encoding: 8bit
一起使用.
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>
";
$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 .= "Importance: 3
";
$headers .= "Date: $date
";
$headers .= "Delivered-to: $to
";
$headers .= "Return-Path: $sender_name<$from>
";
$headers .= "Envelope-from: $sender_name<$from>
";
$headers .= "Content-Transfer-Encoding: 8bit
";
$headers .= "Content-Type: text/plain; charset=UTF-8
";
推薦答案
更新 有關(guān)更實(shí)用和最新的答案,請(qǐng)查看 帕萊克的回答.
Update???For a more practical and up-to-date answer, have a look at Palec’s answer.
Content-Type 中指定的字符編碼只描述了消息體的字符編碼,而不描述消息頭的字符編碼.您需要使用編碼字語(yǔ)法 使用 quoted-printable 編碼 或 Base64 編碼:
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 "?="
您可以將 imap_8bit
用于 quoted-printableem> 編碼和 base64_encode
用于 Base64 編碼:
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)."?="
這篇關(guān)于來(lái)自 PHP 的電子郵件已破壞主題標(biāo)頭編碼的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!