261

I try to use PHPMailer to send registration, activation. etc mail to users:

require("class.phpmailer.php");
$mail -> charSet = "UTF-8";
$mail = new PHPMailer();
$mail->IsSMTP();  
$mail->Host     = "smtp.mydomain.org";  
$mail->From     = "name@mydomain.org";
$mail->SMTPAuth = true; 
$mail->Username ="username"; 
$mail->Password="passw"; 
//$mail->FromName = $header;
$mail->FromName = mb_convert_encoding($header, "UTF-8", "auto");
$mail->AddAddress($emladd);
$mail->AddAddress("mytest@gmail.com");
$mail->AddBCC('mytest2@mydomain.org', 'firstadd');
$mail->Subject  = $sub;
$mail->Body = $message;
$mail->WordWrap = 50;  
if(!$mail->Send()) {  
   echo 'Message was not sent.';  
   echo 'Mailer error: ' . $mail->ErrorInfo;  
}

The $message contains latin characters. Unfortunately all webmail (gmail, webmail.mydomain.org, emailaddress.domain.xx) is using a different coding.

How can I force to use UTF-8 coding to show my mail exactly the same on all mailboxes?

I tried to convert the mail header width mb_convert_encoding(), but without luck.

Synchro
  • 35,538
  • 15
  • 81
  • 104

9 Answers9

617

If you are 100% sure $message contain ISO-8859-1 you can use utf8_encode as David says. Otherwise use mb_detect_encoding and mb_convert_encoding on $message.

Also take note that

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

Should be replaced by:

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

And placed after the instantiation of the class (after the new). The properties are case sensitive! See the PHPMailer doc fot the list & exact spelling.

Also the default encoding of PHPMailer is 8bit which can be problematic with UTF-8 data. To fix this you can do:

$mail->Encoding = 'base64';

Take note that 'quoted-printable' would probably work too in these cases (and maybe even 'binary'). For more details you can read RFC1341 - Content-Transfer-Encoding Header Field.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
AlexV
  • 22,658
  • 18
  • 85
  • 122
  • 48
    Well I don't know if anyone cares as this is crazy old but, I had to add $mail->Encoding = "base64" to get this to work for me – Andrew Revak Jun 23 '15 at 01:32
  • Usage of mb_detect_encoding and mb_convert_encoding would have been nice. – Tigerware Jan 07 '20 at 16:48
  • It is very strange that just this statement was the root cause of the problem, mail was not being sent (but there were no error at all) `$message += '\n\nThis message was sent from IP Address: `. Once I replaced += with .= the emails started getting sent. – TheTechGuy Jun 14 '21 at 00:42
  • 1
    The `$mail->CharSet = 'UTF-8';` trick is still valid as of version 6.5. What a shame that 1. ISO-8859-1 is still the default charset in the PHPMailer class and 2. There's no public method that sets the CharSet public class variable, hence hinting your IDE that such an option exists... – Fabien Haddadi Mar 09 '22 at 18:44
35
$mail -> CharSet = "UTF-8";
$mail = new PHPMailer();

line $mail -> CharSet = "UTF-8"; must be after $mail = new PHPMailer();

try this

$mail = new PHPMailer();
$mail->CharSet = "UTF-8";
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
user2354947
  • 375
  • 3
  • 2
  • 6
    I updated this post for better reading, but the op's question was already answered 2 years before! And that accepted answere included this one already. – Dwza Feb 25 '15 at 16:29
7

I work myself this way

  $mail->FromName = utf8_decode($_POST['name']);

http://php.net/manual/en/function.utf8-decode.php

superhero
  • 6,281
  • 11
  • 59
  • 91
yorkfx
  • 411
  • 5
  • 6
  • Warning This function has been DEPRECATED as of PHP 8.2.0. Relying on this function is highly discouraged. – JSG Feb 25 '23 at 15:17
6

Sorry for being late on the party. Depending on your server configuration, You may be required to specify character strictly with lowercase letters utf-8, otherwise it will be ignored. Try this if you end up here searching for solutions and none of answers above helps:

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

should be replaced with:

$mail->CharSet = "utf-8";
vzr
  • 105
  • 1
  • 7
6

When non of the above works, and still mails looks like ª הודפסה ×•× ×©×œ:

$mail->addCustomHeader('Content-Type', 'text/plain;charset=utf-8');
$mail->Subject = '=?UTF-8?B?' . base64_encode($subject) . '?=';;
Ohad Cohen
  • 5,756
  • 3
  • 39
  • 36
3

I was getting ó in $mail->Subject /w PHPMailer.

So for me the complete solution is:

// Your Subject with tildes. Example.
$someSubjectWithTildes = 'Subscripción España';

$mailer->CharSet = 'UTF-8';
$mailer->Encoding = 'quoted-printable';
$mailer->Subject = html_entity_decode($someSubjectWithTildes);

Hope it helps.

biojazzard
  • 563
  • 4
  • 8
1

To avoid problems of character encoding in sending emails using the class PHPMailer we can configure it to send it with UTF-8 character encoding using the "CharSet" parameter, as we can see in the following Php code:

$mail = new PHPMailer();
$mail->From = 'midireccion@email.com';
$mail->FromName = 'Mi nombre';
$mail->AddAddress('emaildestino@email.com');
$mail->Subject = 'Prueba';
$mail->Body = '';
$mail->IsHTML(true);


// Active condition utf-8
$mail->CharSet = 'UTF-8';


// Send mail
$mail->Send();
0

The simplest way and will help you with is set CharSet to UTF-8

$mail->CharSet = "UTF-8"
Trung Bui
  • 188
  • 1
  • 2
  • 13
0
$mail = new PHPMailer();
$mail->CharSet = "UTF-8";
$mail->Encoding = "16bit";