Home  >  Article  >  Backend Development  >  Summary of solutions to the problem of Chinese garbled emails sent by phpmailer

Summary of solutions to the problem of Chinese garbled emails sent by phpmailer

WBOY
WBOYOriginal
2016-07-25 08:59:553232browse
  1. $mail = new PHPMailer();
Copy code

2: Set the encoding of the email;

  1. $mail->Charset='UTF-8';
Copy code

I believe that many friends set it to "GBK" or "GB2312". I did the same at first, and then I understand that Mail is internationalized. If you want to display Chinese correctly in international mailboxes like Gmail, please set the encoding to "UTF-8";

3,: Set title encoding; Someone may ask here, after setting the encoding of the email, what is the purpose of setting the header encoding? If you have tested multiple mailboxes, you will find that the Chinese title is garbled, yes, even if you set $mail->Charset="UTF-8"; Since learning PHP, you should read more PHP manuals or pay attention to php.net. I found the solution to the garbled title on the PHP official website: hhtp://.www.php.net/mail. $mail->Subject ="Letter title"; changed to $mail->Subject = "=?utf-8?B?" . base64_encode("Letter title") . "?="; "This encoding is designed to enable binary data to be transmitted over a transport layer that is not pure 8-bit, such as the body of an email." (from the PHP manual base64_encode() ) In this way, the Chinese garbled characters in the title will be solved.

#-------------------------- Attachment, there is a problem with garbled Chinese characters in emails sent by phpmailer

The emails sent by phpmailer by default may be garbled. The solution is given online: add a line

  1. $mail->CharSet = "UTF-8";
Copy code

After testing, this method is available for 163, but the QQ mailbox will become garbled, and the QQ mailbox will be garbled by default There is no problem in this case. Correct approach:

  1. $mail->CharSet = "GB2312";
Copy code

For Chinese, it is better to use GB2312. Only QQ has been tested, 163 others have not been tested.

2012-11-5 update: The encoding that should be set in the $mail->CharSet method must be consistent with the encoding of the page where you reference the phpmailer class. For example, if you reference this class in 1.php to send an email, you need to set it to the encoding type of the file 1.php. How to check the encoding of a file? Don’t ask this question. Of course, it is best to use unified file encoding for externally referenced content.

That’s it. I hope the above content can help everyone solve the problem of garbled Chinese emails sent by phpmailer. Programmer's Home, I wish you all a happy study.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn