开发者

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

开发者 https://www.devze.com 2022-12-12 06:43 出处:网络
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 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:

functio开发者_StackOverflown _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...


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.


A mail wrapper such as Swiftmailer might help you.


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" />


You might want to check out PEAR's MAIL_MIME

0

精彩评论

暂无评论...
验证码 换一张
取 消