如何通过 PHP 的邮件功能发送带有阿拉伯语内容的电子邮件? [英] How do I send emails with Arabic content via PHP's mail function?
问题描述
我在使用 PHP 的邮件功能发送包含阿拉伯语内容的电子邮件时遇到了挑战.假设我有这个简单的阿拉伯字符串:
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:
翻译成语
我尝试了几种方法来利用标题,但电子邮件内容仍然以类似的形式结束:X*X1X(X1Y X/
.但是,如果电子邮件主题被正确编码我使用阿拉伯字符(感谢 base64_encode,见下面的函数)
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' ."\r\n";
$headers .= 'To: '.$to ."\r\n";
$headers .= 'From: '.$from . "\r\n";
$headers .= 'Content-type: text/plain; charset=UTF-8; format=flowed' . "\r\n";
$headers .= 'Content-Transfer-Encoding: 8bit'."\r\n";
mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=',$message, $headers);
}
对于实现这一目标的替代方法有什么建议吗?
Any suggestions on alternative ways to achieve this goal?
推荐答案
不幸的是,8bit
编码在电子邮件中并不可靠.许多邮件传输代理会删除邮件正文中每个字节的最高位.بريد
是 UTF-8 字节的 "\xD8\xA8\xD8\xB1\xD9\x8A\xD8\xAF"
;从这些字节中删除最高位,你会得到 ASCII "X(X1Y\nX/"
.
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/"
.
将非 ASCII 字符放入邮件正文的方法是将 Content-Transfer-Encoding
设置为 base64
或 quoted-printable
>,并分别使用 base64_encode
或 quoted_printable_encode
对正文进行编码.
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
如果邮件主要是 ASCII 会更好,因为它保留了编码形式的可读性并且对于 ASCII 更有效.如果整个邮件是阿拉伯语,base64
code> 可能是更好的选择.)
(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.)
这篇关于如何通过 PHP 的邮件功能发送带有阿拉伯语内容的电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!