Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

How do I send emails with Arabic content via PHP's mail function?

I'm having a challenge with sending emails with arabic content using PHP's mail function. Let's say I have this simple arabic string:

????

I've tried several ways to utilize the headers, but the emails content all still end up with something like: X*X1X(X1Y X/. However, the email subject is correctly encoded if I use arabic characters (thanks to the base64_encode, see function below)

Here's one of the email functions I've tried

function sendSimpleMail($to,$from,$subject,$message) {
    $headers = 'MIME-Version: 1.0' ."
";
    $headers .= 'To: '.$to ."
";
    $headers .= 'From: '.$from . "
";
    $headers .= 'Content-type: text/plain; charset=UTF-8; format=flowed' . "
";
    $headers .= 'Content-Transfer-Encoding: 8bit'."
";

    mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=',$message, $headers);
}

Any suggestions on alternative ways to achieve this goal?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Unfortunately, 8bit encoding is not reliable in e-mail. Many mail transport agents will remove the top bit of every byte in the mail body. ???? is "xD8xA8xD8xB1xD9x8AxD8xAF" in UTF-8 bytes; remove the top bit from those bytes and you get ASCII "X(X1Y X/".

The way to get non-ASCII characters into a mail body is to set Content-Transfer-Encoding to either base64 or quoted-printable, and the encode the body with base64_encode or quoted_printable_encode, respectively.

(quoted-printable is better if the mail is largely ASCII as it retains readability in the encoded form and is more efficient for ASCII. If the whole mail is Arabic, base64 would probably be the better choice.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...