PHP Mail Encodes Subject Line

Posted on

PHP Mail Encodes Subject Line – Here in this article, we will share some of the most common and frequently asked about PHP problem in programming with detailed answers and code samples. There’s nothing quite so frustrating as being faced with PHP errors and being unable to figure out what is preventing your website from functioning as it should like php and email . If you have an existing PHP-based website or application that is experiencing performance issues, let’s get thinking about PHP Mail Encodes Subject Line.

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' . "rn";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";

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

$headers .= 'From: <>' . "rn";  

$email_to = $mod_params['email'];

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

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


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

Solution :

Try this:


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.

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.

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:

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.

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

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.

Leave a Reply

Your email address will not be published. Required fields are marked *