Email from PHP has broken Subject header encoding(来自 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-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
";
推荐答案
更新 有关更实用和最新的答案,请查看 帕莱克的回答.
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 的电子邮件已破坏主题标头编码
基础教程推荐
- 使用 PDO 转义列名 2021-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- HTTP 与 FTP 上传 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01