Reputation: 7483
I tried to send a text email with non-English characters using PHPs mail
function. But instead my message went with funny looking garbage characters. How do I fix it?
I use this piece of code:
function _mail($to, $subject, $content)
{
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $content, $headers);
}
Some of the characters came out as question marks...
Upvotes: 0
Views: 4148
Reputation: 149504
The key is to use the UTF-8 character set.
Add Content-Type: text/html; charset=UTF-8
, MIME-Version 1.0
and Content-Transfer-Encoding: quoted-printable
to the mail headers, like this:
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'Content-Type: text/html; charset=UTF-8' . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
'X-Mailer: PHP/' . phpversion(); // Why would you want to send this header?
If you would be using HTML instead of text, you’d also need to add a META
tag to the HEAD
of your (X)HTML mail:
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
Upvotes: 2
Reputation: 992767
This is definitely a case for Joel's article The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!).
You must understand the role of character encodings before you can successfully solve this problem.
Upvotes: 5