沃梦达 / 编程问答 / php问题 / 正文

来自 PHP 的电子邮件已破坏主题标头编码

Email from PHP has broken Subject header encoding(来自 PHP 的电子邮件已破坏主题标头编码)

本文介绍了来自 PHP 的电子邮件已破坏主题标头编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 PHP 脚本向用户发送电子邮件,当电子邮件到达他们的邮箱时,主题行 ($subject) 的末尾添加了类似 a^£ 的字符我的主题文本.这显然是编码问题.电子邮件内容本身没问题,只是主题行坏了.

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.

这是我的标题.请注意,我将 Content-Typecharset=utf-8Content-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
";

推荐答案

更新 有关更实用和最新的答案,请查看 帕莱克的回答.

Update   For a more practical and up-to-date answer, have a look at Palec’s answer.

Content-Type 中指定的字符编码只描述了消息体的字符编码,而不描述消息头的字符编码.您需要使用编码字语法 使用 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)."?="

这篇关于来自 PHP 的电子邮件已破坏主题标头编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:来自 PHP 的电子邮件已破坏主题标头编码

基础教程推荐