Search code examples
phpemailutf-8character-encodingiso

Cannot display french accents in php mail


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.


Solution

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