Ali
Ali

Reputation: 7483

Why are some characters shown as question marks when sending email using non-English characters?

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

Answers (4)

Mathias Bynens
Mathias Bynens

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

ty812
ty812

Reputation: 3323

You might want to check out PEAR's MAIL_MIME

Upvotes: 1

Blair McMillan
Blair McMillan

Reputation: 5349

A mail wrapper such as Swiftmailer might help you.

Upvotes: 3

Greg Hewgill
Greg Hewgill

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

Related Questions