Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 213
Prusprus Avatar asked Dec 06 '22 18:12

Prusprus


2 Answers

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      = '[email protected]';
$subject = 'subject';
$message = utf8_encode('message français ééààèè');
$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

if(mail($to, $subject, $message, $headers)){
echo 'success!';
}
?>
like image 116
bozdoz Avatar answered Dec 09 '22 15:12

bozdoz


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

}
like image 37
J Rem Avatar answered Dec 09 '22 15:12

J Rem