PHP 邮件编码主题行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/447560/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:46:23  来源:igfitidea点击:

PHP Mail Encodes Subject Line

phpemailencoding

提问by ericwindham

When I try to send a HTML encoded email from PHP, if the subject line contains special chars like "Here's the information you requested", PHP encodes it to read "Here's the information you requested."

当我尝试从 PHP 发送 HTML 编码的电子邮件时,如果主题行包含特殊字符,例如"Here's the information you requested",PHP 会将其编码为读取"Here's the information you requested."

How do I fix this?

我该如何解决?



Here's what the code looks like using PHP mail():

下面是使用 PHP mail() 的代码:

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$headers .= 'To: ' . $mod_params['name'] . '<' . $mod_params['email'] . '>' . "\r\n";

$headers .= 'From: <[email protected]>' . "\r\n";  

$email_to = $mod_params['email'];

$email_sub = "Here's the Information You Requested";

$body = html_entity_decode("<html><body>" . $email_html_body . "</body></html>");

mail($email_to,$email_sub,$body,$headers);

It gives the same error as running it through the SugarPHPMailer class.

它给出了与通过 SugarPHPMailer 类运行它相同的错误。

回答by Peter Kopias

Try this:

尝试这个:

 $newsubject='=?UTF-8?B?'.base64_encode($subject).'?=';

This way you don't rely on PHP or the MTA's encoding, you do the job, and the mail client should understand it. No special characters will be present in your new subject, so no problems should arise while delivering the email.

这样您就不必依赖 PHP 或 MTA 的编码,您就可以完成工作,并且邮件客户端应该理解它。您的新主题中不会出现特殊字符,因此在发送电子邮件时不会出现任何问题。

回答by Adam Christianson

I had a similar issue in a Wordpress plug-in I was working on and I racked my brain over and over trying different suggestions from here and in various other Google search results. I finally found a solution that worked in my situation so I'll share it. I will say it was Paul's solution which I tried at first and it didn't work, but the reason was me trying to "shorthand" the solution. In my case just calling html_entity_decode() didn't work. Why? If I had read the PHP doc more closely it would have been obvious. My issue was with encoding on a single quote and the default for html_entity_decode() is 'ENT_COMPAT' which leaves single quotes alone. The solution was to set all the parameters and that worked. In reality I probably could have left off the charset since I was encoding UTF-8, but figured I be thorough.

我在我正在开发的 Wordpress 插件中遇到了类似的问题,我一次又一次地绞尽脑汁尝试来自此处和其他各种 Google 搜索结果的不同建议。我终于找到了一个适用于我的情况的解决方案,所以我会分享它。我会说这是我一开始尝试过的保罗的解决方案,但没有奏效,但原因是我试图“速记”该解决方案。就我而言,仅调用 html_entity_decode() 不起作用。为什么?如果我更仔细地阅读了 PHP 文档,那就很明显了。我的问题是在单引号上进行编码,html_entity_decode() 的默认值是 'ENT_COMPAT',它只留下单引号。解决方案是设置所有参数并且有效。实际上,自从我编码 UTF-8 以来,我可能会忽略字符集,但我认为我是彻底的。

$decoded_str = html_entity_decode (  $value_to_decode, ENT_QUOTES, 'UTF-8' );

The lesson here is a good one, "Read the docs". I'm not saying that you didn't (you probably did), but lot's of us get in a hurry and gloss over the solution which is sitting there staring us in the face if we'd only look.

这里的课程很好,“阅读文档”。我并不是说你没有(你可能有),但我们很多人都会匆忙掩盖解决方案,如果我们只看一眼,就坐在那里盯着我们看。

回答by Michael Haren

If the string really doesn't contain encoded values before you send, take a look at this:

如果在发送之前字符串确实不包含编码值,请查看以下内容

$subject= mb_encode_mimeheader($subject,"UTF-8", "B", "\n");
// or
$subject= mb_encode_mimeheader($subject,"UTF-7", "Q", "\n");


Take a look at these posts related to SugarCRM:

看看这些与 SugarCRM 相关的帖子:

回答by Pascal_dher

You should use mb_encode_mimeheader, just remember to set before.

你应该使用mb_encode_mimeheader,记得之前设置。

mb_internal_encoding("UTF-8"); //has to be set (of course your internal encoding may not be UTF-8).
$subject = mb_encode_mimeheader($subject,'UTF-8','Q');

It will take care of encoding to (the human readable) Quoted-printable when needed and automatically break the subject into the right amount of lines depending on lenght.

它将在需要时处理编码到(人类可读的)引用打印,并根据长度自动将主题分成适当数量的行。

回答by Paul Dixon

Try running the subject line through html_entity_decode(), looks like maybe you have some entities in the subject line.

尝试通过html_entity_decode()运行主题行,看起来主题行中可能有一些实体。

回答by Sampson

Submitting the offending block of code often times will ensure you a better response faster. You are likely encoding the text somewhere before this action takes place. As previously suggested you can seek out that action, and correct it, or you can simply decode the subject line before sending the email.

经常提交有问题的代码块将确保您更快地做出更好的响应。在此操作发生之前,您可能已在某处对文本进行了编码。如前所述,您可以找出该操作并进行更正,或者您可以在发送电子邮件之前简单地解码主题行。