如何通过 PHP 的邮件功能发送包含阿拉伯语内容的电子邮件?

发布于 2024-08-23 03:21:23 字数 663 浏览 7 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

许一世地老天荒 2024-08-30 03:21:23

不幸的是,8bit 编码在电子邮件中并不可靠。许多邮件传输代理会删除邮件正文中每个字节的最高位。 巴里德尔是UTF-8字节的“\xD8\xA8\xD8\xB1\xD9\x8A\xD8\xAF”;删除这些字节中的最高位,您将得到 ASCII "X(X1Y\nX/"

将非 ASCII 字符放入邮件正文的方法是设置 Content-Transfer-Encodingbase64quoted-printable,并使用 base64_encodequoted_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 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.)

场罚期间 2024-08-30 03:21:23
        $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);

这对我有用。

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

束缚m 2024-08-30 03:21:23

试试这个

$headers .= 'From: =?UTF-8?B?'.base64_encode($from). "\r\n";

Try this

$headers .= 'From: =?UTF-8?B?'.base64_encode($from). "\r\n";
清音悠歌 2024-08-30 03:21:23

您的代码按原样对我有用。

您确定 $message 包含有效的 UTF-8 字符串吗?

Your code works for me as-is.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文