ericwindham
ericwindham

Reputation: 201

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

Upvotes: 13

Views: 25166

Answers (6)

Peter Kopias
Peter Kopias

Reputation: 411

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.

Upvotes: 41

Pascal_dher
Pascal_dher

Reputation: 458

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.

Upvotes: 3

Adam Christianson
Adam Christianson

Reputation: 376

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.

Upvotes: 8

Sampson
Sampson

Reputation: 268344

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.

Upvotes: -2

Michael Haren
Michael Haren

Reputation: 108246

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:

Upvotes: 6

Paul Dixon
Paul Dixon

Reputation: 300825

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

Upvotes: -1

Related Questions