如何通过 PHP 的邮件功能发送包含阿拉伯语内容的电子邮件?
我在使用 PHP 的邮件功能发送包含阿拉伯语内容的电子邮件时遇到了挑战。假设我有这个简单的阿拉伯字符串:
我
已经尝试了多种方法来利用标题,但电子邮件内容最终仍然是这样的:X*X1X(X1Y X/
。但是,如果我使用阿拉伯字符,电子邮件主题将被正确编码(感谢 base64_encode,请参阅下面的函数)
这是我尝试过的电子邮件功能之一
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);
}
对于实现此目标的替代方法有什么建议吗?
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' ."\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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不幸的是,
8bit
编码在电子邮件中并不可靠。许多邮件传输代理会删除邮件正文中每个字节的最高位。巴里德尔
是UTF-8字节的“\xD8\xA8\xD8\xB1\xD9\x8A\xD8\xAF”
;删除这些字节中的最高位,您将得到 ASCII"X(X1Y\nX/"
。将非 ASCII 字符放入邮件正文的方法是设置
Content-Transfer-Encoding
为base64
或quoted-printable
,并使用base64_encode
或quoted_printable_encode
对正文进行编码, (如果邮件主要是 ASCII,quoted-printable
更好,因为它保留了编码形式的可读性,并且对于 ASCII 更有效。如果整个邮件是阿拉伯语,
base64
> 可能是更好的选择。)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 eitherbase64
orquoted-printable
, and the encode the body withbase64_encode
orquoted_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.)这对我有用。
This one works for me.
试试这个
Try this
您的代码按原样对我有用。
您确定
$message
包含有效的 UTF-8 字符串吗?Your code works for me as-is.
Are you sure that
$message
contains a valid UTF-8 string?