如何通过PHP的邮件功能发送阿拉伯语内容的邮件? [英] How do I send emails with Arabic content via PHP's mail function?

查看:177
本文介绍了如何通过PHP的邮件功能发送阿拉伯语内容的邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PHP的邮件功能发送带有阿拉伯语内容的电子邮件,我遇到了一个挑战。假设我有这个简单的阿拉伯字符串:



بريد



我尝试过几种方法来使用标题,但电子邮件内容仍然最终如下: X * X1X(X1Y X / 。但是,如果我使用阿拉伯字符,则电子邮件主题正确编码(谢谢base64_encode,请参阅下面的功能)



这是我尝试过的电子邮件功能之一

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

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

任何关于实现这一目标的替代方法的建议?

解决方案

不幸的是, 8bit 编码在电子邮件中不可靠。许多邮件传输代理将删除邮件正文中每个字节的顶部位。 بريد\xD8\xA8\xD8\xB1\xD9\x8A\xD8\xAF UTF-8字节;从这些字节中删除顶部位,您将获得ASCII X(X1Y\\\
X /



将非ASCII字符转换为邮件正文的方式是将 Content-Transfer-Encoding 设置为 base64 quoted-printable ,并使用 base64_encode quoted_printable_encode



quoted-printable 更好的是,如果邮件大部分是ASCII,因为它保留了可读性编码形式,对ASCII更有效,如果整个邮件是阿拉伯语, base64 可能是更好的选择。)


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?

解决方案

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

这篇关于如何通过PHP的邮件功能发送阿拉伯语内容的邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆