Home  >  Article  >  Backend Development  >  The ultimate solution to the problem of garbled title, sender, and content of emails sent and received with PHPMailer

The ultimate solution to the problem of garbled title, sender, and content of emails sent and received with PHPMailer

WBOY
WBOYOriginal
2016-07-25 08:59:401852browse
  1. function EncodeHeader ($str, $position = 'text', $pl = 0) {
  2. if ( $pl ) return "=?" . $this->CharSet . "?B?" . base64_encode ($str) . "?=";
Copy code

is an additional parameter defined for this function.

Naturally, you need to modify the parameters of all places where this function is called. Find: EncodeHeader( Change something like this:

  1. $result .= $this->HeaderLine("Subject", $this->EncodeHeader(trim($this->Subject))));
Copy code

Change become:

  1. $result .= $this->HeaderLine("Subject", $this->EncodeHeader(trim($this->Subject),'text', 1));
Copy The code

means to define the third reference as 1, so that the judgment statement in the function we changed can be called.

Changed this, of course you have to remember to set CharSet=UTF8 when calling this class. In this way, this judgment statement can be converted to UTF8 without being garbled. It can be written like this:

  1. $mail = new PHPMailer();
  2. $mail->CharSet = "utf8";
Copy code

Second: Fix the garbled email title Subject is to process the email title, you need to find this place correctly. Like mine is called like this

  1. $mail = new PHPMailer();
  2. $mail->Subject="XX title";
Copy code

So, like this, change it to this:

  1. $mail->Subject = "=?utf-8?B?".base64_encode("XX title")."?=";
Copy code

is also transcoding ah.

Third: Fix garbled characters in other places The basic principle is the same as the second repair. FromName handles the sender. Where to find the sender's name: Mine is written like this:

  1. $mail = new PHPMailer();
  2. $mail->FromName = "=?utf-8?B?".base64_encode("Yellow Card Network Customer Online Message]")."?=";
Copy code

If you can solve the above three places, the garbled code problem when Phpmailer sends Chinese emails can basically be solved.

Attached is a modified and complete example code for sending emails via phpmailer: http://file.jbxue.com/code/201304/phpmailer_lyb_jbxue.com.zip.



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