Cannot display french accents in php mail

我的未来我决定 提交于 2019-12-07 11:00:35

问题


I have the following php script sends an email based on parameters returned:

<?
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);
?>

I've taken parts out that are not relevent. The message will be sent out fine, but the accents will not appear correctly. Everything has been set as utf-8 charset, i don't understand why this isn't working.


回答1:


You may have to encode the html with utf8_encode(). For example:

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

I have had to do this to dynamically import French Word docs, and it works great. Let me know if this solves your problem.

UPDATE (example working code)

<?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!';
}
?>



回答2:


See here good comments I found. Only this works for me. https://ncona.com/2011/06/using-utf-8-characters-on-an-e-mail-subject/

Details:

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



回答3:


To resolve your issue you need to add the following line to your send email function:

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

Here is the integration of this line with an emailing function:

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

}


来源:https://stackoverflow.com/questions/7983965/cannot-display-french-accents-in-php-mail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!