PHP mail() changing some characters to htmlentities?

304 views Asked by At

My PHP mail() script changes the ! exclamation mark on some email clients such as hotmail to %21 when the ! is in a tag in the email body.

Here's my script

$to = "[email protected]";
$subject = "Password Reset";
$body = "<a href=\"http://example.com/#!/page\">Link 1</a>
        <br><br>
        Without href: http://example.com/#!/page - regular text
    ";
$headers = "From: [email protected]\r\n";
$headers .= "Reply-To: [email protected]\r\n";
$headers .= "Return-Path: [email protected]\r\n";
$headers .= "X-Mailer: PHP5\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
mail($to,$subject,$body,$headers);

So in the script above, the ! is changed to %21 only when it's a link, the regular text keeps it as /#!/instead of transforming it to /#%21/

How would I go about fixing this issue so it doesn't change to %21?

1

There are 1 answers

4
machineaddict On

Some characters are encoded because they are not valid URLs. In your case URL code points are replaced with percent-encoded byte %.