Cannot display french accents in php mail

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

<? 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 accepted the details that are not true. The message will be sent perfectly, but the accents are displayed incorrectly. Everything was installed as utf-8 encoding, I do not understand why this does not work.

+4
source share
3 answers

You may need to encode html with utf8_encode (). For instance:

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

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

UPDATE (working code example)

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

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

More 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); 
+2
source

To solve the problem, you need to add the following line to the email sending function:

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

Here is the integration of this line with the email 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); } 
+1
source

Source: https://habr.com/ru/post/1379345/


All Articles