电子邮件表单中的UTF-8字符

时间:2011-10-23 10:23:06

标签: forms utf-8 character-encoding

以下代码应通过联系表单生成UTF-8编码的电子邮件,该表单将数据发送到相关的数据库表。但是,它仍然没有正确显示UTF-8字符。你能看出我输错了/没有添加/搞砸了下面的代码吗?非常感谢!

<?php
$text = "Results from form:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
echo '<META HTTP-EQUIV=Refresh charset=utf-8 CONTENT="0; URL='.$url.'">';
?>

修改

感谢Chris的快速回复 - 但是,我们的电子邮件表单依赖于包含各种设置的“func.php”文件。目前'sendemail'代码如下:

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: ".$hostmail." \r\n";
$headers .= "Reply-To : ".$hostmail." \r\n";
return mail($to, $subj, $body, $headers);

我试过改变如下,没有成功......有什么想法吗?

$headers .= "From: ".$hostmail." \r\n";
$headers .= "Reply-To : ".$hostmail." \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=utf-8\r\n";
$headers .="Content-Transfer-Encoding: 8bit";
$body=htmlspecialchars_decode($body,ENT_QUOTES);
return mail($to, "=?utf-8?B?".base64_encode($subj)."?=",$body, $headers);

1 个答案:

答案 0 :(得分:4)

这可能有用,也可能没有用,但我觉得我很感激你在处理PHP中的邮件错误方面的痛苦,所以我想我至少会尝试帮助让这个帖子继续下去!

我不想问的一件事是你确定发布的值是以UTF-8编码形式发布的吗?此外,我不禁想到新行字符可能会导致一些时髦的行为,所以为什么不尝试这样的东西(如果适用):

line = "\r\n";
$value = str_replace('\n', $line, $value);

请注意我为$ line添加的新行字符周围的双引号 ...我假设上面的第一个'\ n'实际上被放入了值和需要更换?否则,尤其是使用纯文本电子邮件时,您不必担心解析这些新行字符,只需将其直接输入邮件消息即可。

修改

不确定这是否有帮助,但在使用Zend's mail libraries - UTF-8和base64编码运行最基本的邮件发送功能 - 并检查通过的消息来源之后,这基本上就是您所需要的对于您的纯文本邮件,请将此消息作为消息:

$message = "This is a\r\ntest message";

...邮件来源代码段将是:

Date: Sun, 23 Oct 2011 16:24:16 +0300
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: base64
Content-Disposition: inline
MIME-Version: 1.0

VGhpcyBpcyBhDQp0ZXN0IG1lc3NhZ2U=

编辑#2:

好的,我想我终于想出了一些有用的东西,但这只会在发送带有捷克特色字符的 HTML 邮件时有用。问题似乎与HTML特殊字符的字符编码没有多大关系(好吧,对我来说无论如何!),所以下面的代码似乎在我可靠的Firefox Web浏览器中产生了原始文本:< / p>

<?php

// original message...
$message = "To je nějaký ukázkový text.\r\nTento text je na dalším řádku.";

// convert as many characters as possible in PHP < 5.4...
$message = htmlentities($message, ENT_QUOTES, 'UTF-8');

// I suspect this next line will work for all characters in PHP >= 5.4...
// $message = htmlentities($message, ENT_SUBSTITUTE, 'UTF-8');

// if not all characters are being converted, plug the missing ones in here
// with their relevant codes...
$missing_characters = array
(
    'ě' => '&#283',
    'ř' => '&#345;'
);

// heavy function, but I wasn't trying to be efficient ;)
foreach ($missing_characters as $char => $code)
    $message = str_replace($char, $code, $message);

// this should give you the original string in a browser...
echo $message;

?>

编辑#3:

最后,要在纯文本邮件消息中显示文本以获得相同的结果,要简单得多。只需base64对消息进行编码,设置相应的标头,然后使用PHP's mail函数完成剩下的工作。以下是我如何运作:

// original message...
$message = "To je nějaký ukázkový text.\r\nTento text je na dalším řádku.";
$message = base64_encode($message);

// headers...
$headers = "";
$headers .= "From: your-email@domain.com\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "MIME-Version: 1.0";

mail("destination-email@domain.com", "Subject Here", $message, $headers);

希望这有帮助! :)