php PHP电子邮件编码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2265579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:47:50  来源:igfitidea点击:

PHP E-Mail Encoding?

phpencodingemail

提问by JasonS

I am having some trouble with foreign characters when sending an e-mail. Could someone advise me on what to do? I suspect the problem could be one of three things.

我在发送电子邮件时遇到了一些外来字符的问题。有人可以建议我该怎么做吗?我怀疑问题可能是三件事之一。

  1. The html page encoding is incorrect. (Would this affect the POST data from the form?)
  2. The mail function doesn't have any encoding. Thus the program doesn't know how to read it. (Most likely)
  3. The file itself doesn't have the right encoding and thus is making problems. (Probably quite unlikely)
  1. html 页面编码不正确。(这会影响表单中的 POST 数据吗?)
  2. 邮件功能没有任何编码。因此程序不知道如何读取它。(最有可能的)
  3. 文件本身没有正确的编码,因此会产生问题。(大概不太可能)

Are there any other possible causes?

还有其他可能的原因吗?

I am trying to knock these out 1 by 1 until I find the problem. I think that option 2 is the most likely cause. How do I add proper - universal encoding to a mail function?

我正在尝试将它们一一排除,直到找到问题为止。我认为选项 2 是最可能的原因。如何向邮件功能添加正确的通用编码?

This is what I have at the moment.

这就是我目前所拥有的。

$mail_sent = mail($client_email, $title, $message, "From: {$visitor_email}");

I am currently aware that form does not send polish or Swedish characters.

我目前知道该表单不会发送波兰语或瑞典语字符。

I would be very grateful if someone could point out any other possible causes and tell me what encoding I need to use to send e-mails.

如果有人能指出任何其他可能的原因并告诉我发送电子邮件需要使用什么编码,我将不胜感激。

Thanks a lot.

非常感谢。

采纳答案by Belrog

As far as I know PHP does not support UTF-8 as default encoding for its strings. You need to use the relevant encoding/handling functions for the encoding you would prefer.

据我所知,PHP 不支持 UTF-8 作为其字符串的默认编码。您需要为您喜欢的编码使用相关的编码/处理函数。

Also add a Content-Type:text/html;charset=utf-8to your email headers so the email clients will display the characters correctly (or replace with your encoding of choice).

还要Content-Type:text/html;charset=utf-8在您的电子邮件标题中添加一个,以便电子邮件客户端正确显示字符(或替换为您选择的编码)。

回答by Gumbo

You didn't specify the type and encoding of your content. Try this:

您没有指定内容的类型和编码。尝试这个:

$headerFields = array(
    "From: {$visitor_email}",
    "MIME-Version: 1.0",
    "Content-Type: text/html;charset=utf-8"
);
$mail_sent = mail($client_email, $title, $message, implode("\r\n", $headerFields));

回答by Yanni

Use this code

使用此代码

function mail_send($arr)
{
    if (!isset($arr['to_email'], $arr['from_email'], $arr['subject'], $arr['message'])) {
        throw new HelperException('mail(); not all parameters provided.');
    }

    $to      = empty($arr['to_name']) ? $arr['to_email'] : '"' . mb_encode_mimeheader($arr['to_name']) . '" <' . $arr['to_email'] . '>';
    $from    = empty($arr['from_name']) ? $arr['from_email'] : '"' . mb_encode_mimeheader($arr['from_name']) . '" <' . $arr['from_email'] . '>';

    $headers = array
    (
        'MIME-Version: 1.0',
        'Content-Type: text/html; charset="UTF-8";',
        'Content-Transfer-Encoding: 7bit',
        'Date: ' . date('r', $_SERVER['REQUEST_TIME']),
        'Message-ID: <' . $_SERVER['REQUEST_TIME'] . md5($_SERVER['REQUEST_TIME']) . '@' . $_SERVER['SERVER_NAME'] . '>',
        'From: ' . $from,
        'Reply-To: ' . $from,
        'Return-Path: ' . $from,
        'X-Mailer: PHP v' . phpversion(),
        'X-Originating-IP: ' . $_SERVER['SERVER_ADDR'],
    );

    mail($to, '=?UTF-8?B?' . base64_encode($arr['subject']) . '?=', $arr['message'], implode("\n", $headers));
}

from:http://php.net/manual/en/function.mail.php

来自:http: //php.net/manual/en/function.mail.php

回答by teemoo

In addition to what was said earlier, it is not mandatory to send the mail in HTML to be able to use UTF-8, you may also format it as simple text in the headers:

除了前面所说的,为了能够使用 UTF-8 格式发送邮件不是强制性的,您也可以将其格式化为标题中的简单文本:

'Content-Type: text/plain;charset=utf-8'

回答by Atara

I use the following code:

我使用以下代码:

  $text = "";
  $text .= "<html>\n";
  $text .= "<body style=\"font-family:Arial; \">\n";

  $text .= "<b>Add Your text here . . .</b>";
  $text .= date("d.m.Y") . " <br><br>\n\n";

  $text .= "</body>\n";
  $text .= "</html>\n";

$headers = 'From: ' . $myEmail . "\r\n";
$headers .= 'To: ' . $depEmail . "\r\n";    
$headers .= 'Return-Path: ' . $myEmail . "\r\n";
$headers .= 'MIME-Version: 1.0' ."\r\n";
$headers .= 'Content-Type: text/HTML; charset=ISO-8859-1' . "\r\n";
$headers .= 'Content-Transfer-Encoding: 8bit'. "\n\r\n";
$headers .= $text . "\r\n";

set_time_limit(30);
if (!mail('', 'Demande information', '', $headers)) {
  Alert(...)

回答by Piskvor left the building

Use a PHP mail wrapper, it will save your sanity (speaking from experience here). For example, PHPMailerallows you to set outgoing e-mail encoding and builds the message for you.

使用 PHP 邮件包装器,它将节省您的理智(从这里的经验说起)。例如,PHPMailer允许您设置外发电子邮件编码并为您构建消息。

Also, use UTF-8; it's almost universally supported nowadays and covers all the characters you would ever need.

另外,使用 UTF-8;它现在几乎得到普遍支持,涵盖了您需要的所有字符。

回答by isboleas

I use this code to solve the problem with greek encode in old Horde mail.

我使用此代码来解决旧部落邮件中希腊语编码的问题。

$headers = 'From: ' . $youremail . "\r\n";
$headers .= 'To: ' . $to . "\r\n";    
$headers .= 'Return-Path: ' . $youremail . "\r\n";
$headers .= 'MIME-Version: 1.0' ."\r\n";
$headers .= 'Content-Type: text/HTML; charset=utf-8' . "\r\n";
$headers .= 'Content-Transfer-Encoding: 8bit'. "\n\r\n";
$headers .= $message . "\r\n";

mail('', 'Request from Site yoursite', '', $headers);