cppit
cppit

Reputation: 4564

php mail() function changes characters and removes return breaks

this is what I have :

$msg = "1'2 ’3"4 “5”6 7~8!9@10#11$12%13^14&15*16(17)18}19{20"21:22?23>24<25 ";
mail("[email protected]","My subject",$msg);

when I receive the email I get this :

1&#039;2 ’3&quot;4 “5”6 7~8!9@10#11$12%13^14&amp;15*16(17)18}19{20&quot;21:22?23&gt;24&lt;25

so it changes the characters to html I guess? any ideas? I need the email received to contain exactly what I had stored in $msg.thank you

it also removed return breaks I had. in the db the field has return breaks it is stored like this:

Hello $name

welcome to $websitename 

thank you, management

this is a lame example,but as you can see there are 2 empty lines. when the email is sent out those return breaks are ignored and its all in line any thoughts please

Upvotes: 5

Views: 177

Answers (2)

Ja͢ck
Ja͢ck

Reputation: 173642

You have to encapsulate the message inside base64 encoded format to prevent content mangling:

$msg = "1'2 ’3\"4 “5”6 7~8!9@10#11$12%13^14&15*16(17)18}19{20\"21:22?23>24<25";

$body = chunk_split(base64_encode($msg));

mail("[email protected]", "My subject", $body, "Content-Transfer-Encoding: base64");

The way newlines are handled varies amongst different mail clients; for instance, Outlook is known to squash newlines but also offers a way to restore them.

For complete control you should go for HTML emails instead, optionally with text mode fallback.

Upvotes: 2

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100195

Try adding headers, like:

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $subject, $message, $headers);

See here: PHP mail()

Upvotes: 4

Related Questions