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 sev
$boundary = uniqid(rand(), true);
$headers = "From: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative; boundary = $boundary\n";
$headers .= "This is a MIME encoded message.\n\n";
$headers .= "--$boundary\n" .
"Content-Type: text/plain; charset=UTF-8 \n" .
"Content-Transfer-Encoding: base64\n\n";
$headers .= chunk_split(base64_encode($plaintext));
$headers .= "--$boundary\n" .
"Content-Type: text/html; charset=ISO-8859-1\n" .
"Content-Transfer-Encoding: base64\n\n";
$headers .= chunk_split(base64_encode($msg));
$headers .= "--$boundary--\n" .
mail($address, $subject, '', $headers);
This one works for me.
Try this
$headers .= 'From: =?UTF-8?B?'.base64_encode($from). "\r\n";
Your code works for me as-is.
Are you sure that $message
contains a valid UTF-8 string?
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 "\xD8\xA8\xD8\xB1\xD9\x8A\xD8\xAF"
in UTF-8 bytes; remove the top bit from those bytes and you get ASCII "X(X1Y\nX/"
.
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.)