Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

php - PHPMailer character encoding issues

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     = "[email protected]";
$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("[email protected]");
$mail->AddBCC('[email protected]', '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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...