Phpmailer: utf-8 special charatecters problem with some email clients sending mail with inline images (base64 encoded)

Created on 22 Sep 2014  ·  16Comments  ·  Source: PHPMailer/PHPMailer

Hi,
First of all I would like to thank you for your great work.
I have a problem with some email clients when sending email in html utf-8 that contain special characters and i insert one or more inline images (base64 encoded).
It works good with Thunderbird, Gmail, Opera Mail but in Windows Mail and MS Outlook for example the text: "èé@àààùè" became "èé à à ùè". If I don't add inline images work well with all the client i tested.

Most helpful comment

You've not posted your code so I can't tell what you're doing. The default charset is ISO-8859-1, so you need to set $mail->CharSet = 'utf-8'; to work in UTF-8.

All 16 comments

You've not posted your code so I can't tell what you're doing. The default charset is ISO-8859-1, so you need to set $mail->CharSet = 'utf-8'; to work in UTF-8.

I already use it $mail->CharSet = 'utf-8' after $mail = new PHPMailer();
the code:

$datura = strftime("%A %d %B %Y");

$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$mail->setFrom('[email protected]', 'Newsletter');
$mail->addAddress([email protected], 'yyyy');

$mail->Subject = utf8_encode('Le principali tendenze del packaging ...  '.$datura);

$body  = file_get_contents('contents.html');

$mail->msgHTML($body, dirname(__FILE__));

$mail->AltBody = 'Le ultime notizie sul mondo del beverage';

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo . ' ' . $mailsingola . '<br />';
} else {
    echo "Message sent! " . $mailsingola;
}

It works correctly with all email clients. It does not work only when I insert one or more inline images in the contents.html file, and only with Windows Mail and Outlook as i tested. The html file contains <meta http-equiv="Content-Type" content="text/html; charset=utf-8">.

meta http-equiv="Content-Type" content="text/html; charset=utf-8"

Why are you calling utf8_encode? Isn't your content already in UTF-8?

Is UTF-8 working anywhere in the message, for example in the subject line? Can you post complete messages (perhaps in a gist rather than here) with and without the images so we can compare them?

Please can you check out the master branch which has some changes relating to this and give it a try - I've tried various things but I can't make it go wrong as you describe. See also #377

Closing due to no feedback.

Hi,

I've read a lovely book recently, the author is admired by everyone, you should read the book too, that's for sure, here is the link http://quoshuzyque.5fingerlickin.com/e4dtcpil

Very truly yours, ljlorente

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

Hi Synchro,
I work as a French City Hall webmaster and we have a trouble receiving emails with french accents.
I went everywhere on your Github phpmailer and I have done what you explained like:
-$mail->CharSet = 'utf-8';
-All the html pages are in utf8
mbstring is on
And nothing is OK the accents are not OK and when there is accents in the body message, the message body is empty ;-)
I use phpmailer 5.2.20 and I cannot change to 6.0.6 as I will have to modify the whole things

<?php
session_start();

include("php/abcdefghikjl/PHPMailerAutoload.php");

$to = '[email protected]';

if(isset($_POST['emailSent'])) {

    $subject = utf8_decode($_POST[‘subject’]);

    if (strtolower($_POST["captcha"]) == strtolower($_SESSION['captcha']['code'])) {

        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $message = utf8_decode($message);

        // Step 3 - Configure the fields list that you want to receive on the email.
        $fields = array(
            0 => array(
                'text' => 'Nom',
                'val' => $_POST['name']
            ),
            1 => array(
                'text' => 'Adresse Email',
                'val' => $_POST['email']
            ),
            2 => array(
                'text' => 'Message',
                'val' => $_POST['message']
            ),
            3 => array(
                'text' => 'Objet de ce Message',
                'val' => $_POST['select']
            ),
            4 => array(
                'text' => '-Raisons de votre Demande',
                'val' => implode($_POST['checkboxes'], ", ")
            ),
            5 => array(
                'text' => '-Qui souhaitez-vous Rencontrer',
                'val' => $_POST['radios']
            )
        );

        $message = "";

        foreach($fields as $field) {
            $message .= $field['text'].":  " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
        }

        $mail = new PHPMailer;
        $mail->isHTML(true);
        $mail->CharSet = 'utf-8';

        //$mail->IsSMTP();                                      // Set mailer to use SMTP
        //$mail->SMTPDebug = 0;                                 // Debug Mode

        $mail->Host = 'mail.domain.fr';               // Specify main and backup server
        $mail->SMTPAuth = true;                             // Enable SMTP authentication
        $mail->Username = '[email protected]';               // SMTP username
        $mail->Password = 'secretpassword';                         // SMTP password
        $mail->SMTPSecure = 'tls';                          // Enable encryption, 'ssl' also accepted
        $mail->Port = 587;                                    // TCP port to connect to

        $mail->From = $email;
        $mail->FromName = $_POST['name'];
        $mail->AddAddress($to);
        $mail->AddReplyTo($email, $name);

        $mail->IsHTML(true);


        $mail->Subject = $subject;
        $mail->Body    = $message;




        if($mail->Send()) {
            $arrResult = array('response'=> 'success');
        } else {
            $arrResult = array('response'=> 'error', 'error'=> $mail->ErrorInfo);
        }

    } else {

        $arrResult['response'] = 'captchaError';

    }

}
?>

And my message input is:

<div class="row">
                                    <div class="form-group">
                                        <div class="col-md-12">
                                            <label>Message</label>
                                            <textarea maxlength="1000" data-msg-required="Merci de saisir votre Message" rows="5" class="form-control" name="message" id="message" required></textarea>
                                        </div>
                                    </div>
                                </div>

Please, can help do solving this accent troubles as users are complaining to the City Hall and I do not know what to answer
I really thank you if you may help
David

Bonjour David,

The problem is that you are doing this:

$subject = utf8_decode($_POST[‘subject’]);
$message = utf8_decode($message);

utf8_decode converts valid UTF-8 text into the ISO-8859-1 charset, which is absolutely not what you want! You want everything in UTF-8, all the time - all your pages, scripts, editors, everything. If your'e doing that then it should "just work" by setting CharSet = 'utf-8'.

I also note in that $_POST[‘subject’] line above it's using the wrong kind of quotes.

Bonjour Synchro,
I'll try this and I'll let you know
Thanks a lot for your time ;-) et MERCI ;-)
David

Hi again @Synchro I am really sorry to inform you that the message body is empty ;-)
` Nom: David Axxxxxxxxx Adresse Email: [email protected] **Message:** Objet de ce Message: **Demenagement** -Raisons de votre Demande: Mutation Definitive, Mutation Temporaire, Retraite -Qui souhaitez-vous Rencontrer: Antoine
As you may see, **Message is empty** and ### Demenagement is without accents like **Déménagement** And I strictly done what you told me. _Underneath do find the form_ session_start();

include("php/@7PSBYyAFFDzzU64/PHPMailerAutoload.php");

$to = '[email protected]';

if(isset($_POST['emailSent'])) {

$subject = $_POST['subject'];

if (strtolower($_POST["captcha"]) == strtolower($_SESSION['captcha']['code'])) {

    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];


    // Step 3 - Configure the fields list that you want to receive on the email.
    $fields = array(
        0 => array(
            'text' => 'Nom',
            'val' => $_POST['name']
        ),
        1 => array(
            'text' => 'Adresse Email',
            'val' => $_POST['email']
        ),
        2 => array(
            'text' => 'Message',
            'val' => $_POST['message']
        ),
        3 => array(
            'text' => 'Objet de ce Message',
            'val' => $_POST['select']
        ),
        4 => array(
            'text' => '-Raisons de votre Demande',
            'val' => implode($_POST['checkboxes'], ", ")
        ),
        5 => array(
            'text' => '-Qui souhaitez-vous Rencontrer',
            'val' => $_POST['radios']
        )
    );

    $message = "";

    foreach($fields as $field) {
        $message .= $field['text'].":  " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
    }

    $mail = new PHPMailer;
    $mail->isHTML(true);
    $mail->CharSet = 'utf-8';

    //$mail->IsSMTP();                                      // Set mailer to use SMTP
    //$mail->SMTPDebug = 0;                                 // Debug Mode

    $mail->Host = 'mail.domain.fr';               // Specify main and backup server
    $mail->SMTPAuth = true;                                        // Enable SMTP authentication
    $mail->Username = '[email protected]';               // SMTP username
    $mail->Password = 'xxxxxxxxxxxxxxxxxx';                 // SMTP password
    $mail->SMTPSecure = 'tls';                                        // Enable encryption, 'ssl' also accepted
    $mail->Port = 587;                            // TCP port to connect to

    $mail->From = $email;
    $mail->FromName = $_POST['name'];
    $mail->AddAddress($to);
    $mail->AddReplyTo($email, $name);

    $mail->IsHTML(true);



    $mail->Subject = $subject;
    $mail->Body    = $message;




    if($mail->Send()) {
        $arrResult = array('response'=> 'success');
    } else {
        $arrResult = array('response'=> 'error', 'error'=> $mail->ErrorInfo);
    }

} else {

    $arrResult['response'] = 'captchaError';

}

}
?>`
Synchro, I am really sorry to bother you again with this kind of little troubles,
I may also give you all my credentials if you may take time to see what's going on
Thanks again for helping me MERCI
David

You need to be absolutely sure where the text is going astray, so test using fixed strings instead of your form, for example:

$mail->Subject = 'Déménagement';

A separate problem is this:

$mail->From = $email;

That is forgery and will result in your messages being spam-filtered or bounced. Put your own address in there and use the submitter's address as a reply-to address (which you're already doing).

OK @Synchro I'll do what you answered me and I'll tell you ;-)
Merci beaucoup, il fait beau à Sallanches ??

@Synchro
Je viens de tester avec les paramètres que vous m'avez fourni tel que:
$mail->Subject = 'Déménagement';
Et le sujet dans l'email est vide et me corps du message aussi et pourtant j'ai rempli avec des accents.
Désolé encore de vous importuner avec cela mais je suis vraiment dans la m........
Je suis le webmaster d'une commune en Corse et la Mairie commence à râler un maximum
Pouvez-vous m'aider encore un peu
Merci beaucoup
David
Petite précision: Il n'y a que le message qui pose problème et dans le html du message, voilà ce qu'il y a:
<div class="col-md-12"> <label>Message</label> <textarea maxlength="1000" data-msg-required="Merci de saisir votre Message" rows="5" class="form-control" name="message" id="message" required></textarea> </div>

Hi @Synchro
Thank you 👍 so much as It works for now and It was my fault ;-)
I had introduce in my Centos 7 specific php like 8859-1 and both iso-8859-1 and UTF-8 were doing the job ;-) ;-)
Thanks again for your help
David

Oui, il fait une belle journée 🏔☀️ - pas comme mon français! Je suis heureux vous avez résolu le problème!

Was this page helpful?
0 / 5 - 0 ratings