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

后端 未结 4 1686
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 13:11

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

相关标签:
4条回答
  • 2020-11-30 13:42
            $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.

    0 讨论(0)
  • 2020-11-30 13:48

    Try this

    $headers .= 'From: =?UTF-8?B?'.base64_encode($from). "\r\n";
    
    0 讨论(0)
  • 2020-11-30 13:57

    Your code works for me as-is.

    Are you sure that $message contains a valid UTF-8 string?

    0 讨论(0)
  • 2020-11-30 14:01

    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.)

    0 讨论(0)
提交回复
热议问题