0

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: info@example.com' . "\r\n" .
    'Reply-To: info@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

 mail($to, $subject, $content, $headers);
}

Some of the characters came out as question marks...

Arjan
  • 22,808
  • 11
  • 61
  • 71
Ali
  • 7,353
  • 20
  • 103
  • 161
  • 1
    Please show us what you tried? And the result of a small test message, including its headers? (See http://superuser.com/questions/66082/why-do-russian-characters-in-some-received-emails-change-when-reading-in-david/66104#66104 if you don't know what I mean.) – Arjan Nov 21 '09 at 10:18
  • possible duplicate of [What is character encoding and why should I bother with it](http://stackoverflow.com/questions/10611455/what-is-character-encoding-and-why-should-i-bother-with-it) – Raedwald Apr 10 '15 at 12:37

4 Answers4

5

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.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
3

A mail wrapper such as Swiftmailer might help you.

Blair McMillan
  • 5,299
  • 2
  • 25
  • 45
  • I'm using SwiftMailer, and the same problem as OP's led me here. Characters čćžšđČĆŽŠĐ each turn into a "??" (double question mark). Using `$msg = (new Swift_Message('subject'))->setFrom([me@example.com])->setTo([you@example.com])->setBody($htmlMailBody, 'text/html', 'utf-8');` – s3c Oct 05 '21 at 11:02
2

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: info@example.com' . "\r\n" .
           'Reply-To: info@example.com' . "\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" />
Arjan
  • 22,808
  • 11
  • 61
  • 71
Mathias Bynens
  • 144,855
  • 52
  • 216
  • 248
  • 3
    You will also need `MIME-Version: 1.0` and to `Content-Transfer-Encoding: quoted-printable` the body, as UTF-8 characters typically won't make it through the mail system intact otherwise. – bobince Nov 21 '09 at 13:12
  • But obviously, when using quoted-printable, then the message should be encoded as such as well. – Arjan Nov 21 '09 at 15:25
1

You might want to check out PEAR's MAIL_MIME

ty812
  • 3,293
  • 19
  • 36