PHP Mail Encodes Subject Line

谁说胖子不能爱 提交于 2019-12-28 05:52:16

问题


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."

How do I fix this?


Here's what the code looks like using 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: <do_not_reply@a4isp.com>' . "\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.


回答1:


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.




回答2:


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.

$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.




回答3:


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:

  • http://www.sugarcrm.com/forums/showthread.php?t=11940
  • http://www.sugarcrm.com/forums/showthread.php?t=11106&highlight=iso-8859-1



回答4:


You should use mb_encode_mimeheader, just remember to set before.

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.




回答5:


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




回答6:


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.



来源:https://stackoverflow.com/questions/447560/php-mail-encodes-subject-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!