Stéphane Robert
Stéphane Robert

Reputation: 97

UTF-8 Accents not been well displayed in HTML email with PHPMailer

I'm setup a PHPMailer PHP Form that send us the form to our Office 365 account. Im having issue with French accents displayed has "ééé à à à çç" accents like "éé àà çç".

PHP Form are encoded in UTF-8; PHP Code are also encoded in UTF-8;

But the email received seems to not show the proper characters.

I have add theses settings and nothing has changed :

In the PHP file

header('Content-Type: charset=utf-8');

Also

$mail->isHTML(true);     // Set email format to HTML
$mail->CharSet = "UTF-8";

Php Sending Form Source Code:

<?php
header('Content-Type: charset=utf-8');
ini_set('startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(E_ALL);    

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'php/phpmailer/vendor/phpmailer/phpmailer/src/Exception.php';
require 'php/phpmailer/vendor/phpmailer/phpmailer/src/PHPMailer.php';
require 'php/phpmailer/vendor/phpmailer/phpmailer/src/SMTP.php';

$nom_compagnie = $_POST['nom_compagnie'];
$nom_complet = $_POST['nom_complet'];
$poste = $_POST['poste'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$commentaire = $_POST['commentaire'];
$from = $_POST['email'];

function post_captcha($user_response) {
    $fields_string = '';
    $fields = array(
        'secret' => 'PrivateKey',
        'response' => $user_response
    );
    foreach($fields as $key=>$value)
    $fields_string .= $key . '=' . $value . '&';
    $fields_string = rtrim($fields_string, '&');

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
    curl_setopt($ch, CURLOPT_POST, count($fields));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);

    $result = curl_exec($ch);
    curl_close($ch);

    return json_decode($result, true);
}
    $res = post_captcha($_POST['g-recaptcha-response']);

if (!$res['success']) {
    // What happens when the reCAPTCHA is not properly set up
    echo 'reCAPTCHA error: Check to make sure your keys match the registered domain and are in the correct locations. You may also want to doublecheck your code for typos or syntax errors.';

} else {
    // If CAPTCHA is successful...
    try {
    $mail = new PHPMailer(true);
    $mail->isSMTP();
    $mail->Host = 'smtp.office365.com';
    $mail->Port = 587;
    $mail->SMTPSecure = 'tls';
    $mail->SMTPAuth = true;
    $mail->Username = 'EmailAccount';
    $mail->Password = 'Password';
    $mail->addReplyTo($from, $nom_complet);
    $mail->SetFrom ("Hidden", "Hidden");
    $mail->addCC ("Hidden", "Hidden");
    $mail->addAddress ('Hidden', 'Hidden);
    //$mail->SMTPDebug = 3;
    //$mail->Debutoutput = fonction($str, $level) {echo "debug level $level; message: $str";}; //

    $mail->isHTML(true);     // Set email format to HTML
    $mail->Subject = "FORMULAIRE CONTACT";
    $mail->Body    = "
    <html>
    <head>
    <meta charset=\"UTF-8\">
    <title>Formulaire de Contact</title>
    ....
    </html>";
    $mail->AltBody = "";
    $mail->CharSet = "UTF-8";

    //$mail->msgHTML(file_get_contents('email_form_contact-fr.html'));
    $mail->send();
    // Paste mail function or whatever else you want to happen here!
    } catch (Exception $e) {
        echo $e->getMessage();
        die(0);
    }
        header('Location: envoi_form_contact-success-fr.html');
}

?>

The email received has shown like this :

The H3 title in the email are shown

Vous avez reçu un formulaire de contact via le site Web

It supposed to be written like this

Vous avez reçu un formulaire de contact via le site web

Accent "é" are also displayed has "é".

I don't know where is the problem.

Any clue if my code are well programmed?

Thanks.

Upvotes: 0

Views: 4543

Answers (2)

Alex Massy
Alex Massy

Reputation: 36

Replace

$mail -> charSet = "UTF-8"; 

with

$mail->CharSet = 'UTF-8'; 

Upvotes: 2

Synchro
Synchro

Reputation: 37710

Bonjour Stéphane. This page describes the symptom you are seeing; one character turning into two means that your data is in UTF-8, but is being displayed using an 8-bit character set. Generally speaking, PHPMailer gets this right, so you need to figure out where you are going wrong.

If you use SMTPDebug = 2 you will be able to see the message being sent (use a really short message body like é😎 that is guaranteed to only work in UTF-8).

Make sure that the encoding of the script file itself is also UTF-8 - putting an emoji in it is a good way of being sure that it is.

The problem with diagnosing this is that you'll find your OS interferes - things like copying to the clipboard are likely to alter encodings - so the way to deal with it is to use a hex dump function that lets you inspect the actual byte values. French is one of the easier ones to look at because nearly all characters will be regular single-byte ASCII-compatible characters, and accented characters will be easier to spot. In PHP the bin2hex() function will do this.

You have 2 typos: Debutoutput should be Debugoutput and fonction should be function - and that is a good place to dump hex data from:

$mail->Debugoutput = function($str, $level) {echo $str, "\n", bin2hex($str), "\n";};

Here's an example:

echo 'abc ', bin2hex('abc');

which will produce abc 616263; each input char results in 2 hex digits (1 byte) of output. If your input is abé and your charset is ISO-8859-1, it will come out as abé 6162e9, because é in that charset is e9 (in hex). The same string in UTF-8 would come out as abé 6162c3a9, because é in UTF8 is c3a9 - two bytes, not just one. Inspecting the characters like this allows you to be absolutely certain what character set the data is in - just looking at it is not good enough!

So, while I can't tell exactly where your problem is, hopefully you have some better ideas of how to diagnose it.

Upvotes: 0

Related Questions