Search code examples
phpmailer

PHPMailer and specific characters in subject (UTF-8?)


I'm trying to send email with PHPMailer. Everythings work except UTF-8 encoding in Subject. I still get html code (commancé and has to be "commancé") in my mail client.

$mail = new PHPMailer(true);
$mail->CharSet = 'UTF-8';
$mail->Encoding = 'base64';

try {
    //Server settings

    //Recipients
    $mail->setFrom('[email protected]', 'Kevin de Exple');
    $mail->addAddress($email, $name);     // Add a recipient
    
    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $subject = 'RE: La plantation de votre arbre a commancé';
    $sub = '=?UTF-8?B?'.base64_encode($subject).'?=';
    $mail->Subject = $sub;

...

Could you help me? I tried everything on the web :)

Have a good day!


Solution

  • I'm not sure why you're trying to do things the hard way! The first step is setting the CharSet property to UTF-8, which you've done. For the subject, you have this:

    $subject = 'RE: La plantation de votre arbre a commancé';
    $sub = '=?UTF-8?B?'.base64_encode($subject).'?=';
    $mail->Subject = $sub;
    

    There's a lot of unnecessary stuff going on in there. All you need to do is:

    $mail->Subject = 'RE: La plantation de votre arbre a commancé';
    

    PHPMailer takes care of all the encoding for you. The only thing to be careful of here is to be sure that you are actually working in UTF-8 in your editor. If you're using ISO-8859-1 or similar, it won't work – though it will look identical in your code.

    As for spelling mistakes, I'm going to have to leave them up to you...