无法在php邮件中显示法语口音

时间:2011-11-02 16:37:57

标签: php email utf-8 character-encoding iso

我有以下php脚本根据返回的参数发送电子邮件:

<?
header('Content-Type: application/json; charset=utf-8');
$headers  = "From: Source\r\n";
    $headers .= "Content-type: text/html;charset=utf-8\r\n";
    $to = $data["t_email"];
    $subject = "Hello";
    $message = (gather_post("locale") == "fr_CA")?"message français ééààèè": "english message";
    mail($to, $subject, $message, $headers);
?>

我已经采取了不相关的部分。邮件将被正常发送,但重音符号将无法正确显示。一切都被设置为utf-8 charset,我不明白为什么这不起作用。

3 个答案:

答案 0 :(得分:7)

您可能必须使用utf8_encode()对html进行编码。例如:

$message = utf8_encode("message français ééààèè");

我必须这样做才能动态导入法语Word文档,而且效果很好。如果这可以解决您的问题,请告诉我。

更新(示例工作代码)

<?php
$to      = 'example@gmail.com';
$subject = 'subject';
$message = utf8_encode('message français ééààèè');
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

if(mail($to, $subject, $message, $headers)){
echo 'success!';
}
?>

答案 1 :(得分:3)

在这里看到我发现的好评。只有这对我有用。 https://ncona.com/2011/06/using-utf-8-characters-on-an-e-mail-subject/

详细说明:

to = 'example@example.com';
$subject = 'Subject with non ASCII ó¿¡á';
$message = 'Message with non ASCII ó¿¡á';
$headers = 'From: example@example.com'."\r\n"
.'Content-Type: text/plain; charset=utf-8'."\r\n";
mail($to, '=?utf-8?B?'.base64_encode($subject).'?=', $message, $headers);

答案 2 :(得分:1)

要解决您的问题,您需要将以下行添加到您的发送电子邮件功能:

$headers .= 'Content-type: text/plain; charset=UTF-8' . "\r\n";

以下是此行与电子邮件功能的集成:

function send_email($to,$subject,$message,$fromemail) {

    $headers = "From: $fromemail" . "\r\n";
    $headers .= "Return-Path: $fromemail" . "\r\n";
    $headers .= "Errors-To: $fromemail" . "\r\n";
    $headers .= 'Content-type: text/plain; charset=UTF-8' . "\r\n";
    @mail($to,$subject,$message,$fromemail);

}