We have introduced PHPMailer's usage and instance code. some friends found that when sending a Chinese email, the title and content are garbled characters. how can this problem be solved?
We have introduced PHPMailer's usage and instance code. some friends found that when sending a Chinese email, the title and content are garbled characters. how can this problem be solved?
Let's take a look at the previous example code mail. php file:
Require 'class. phpmailer. php ';
$ Mail = new PHPMailer;
$ Mail-> IsSMTP (); // Set mailer to use SMTP
$ Mail-> Host = 'smtp1 .example.com; smtp2.example.com '; // Specify main and backup server
$ Mail-> SMTPAuth = true; // Enable SMTP authentication
$ Mail-> Username = 'jswanc'; // SMTP username
$ Mail-> Password = 'secret'; // SMTP password
$ Mail-> SMTPSecure = 'tls '; // Enable encryption, 'SSL' also accepted
$ Mail-> From = 'from @ example.com ';
$ Mail-> FromName = 'mailer ';
$ Mail-> AddAddress ('Josh @ example.net ', 'Josh Adams'); // Add a recipient
$ Mail-> AddAddress ('Ellen @ example.com '); // Name is optional
$ Mail-> AddReplyTo ('info @ example.com ', 'information ');
$ Mail-> AddCC ('CC @ example.com ');
$ Mail-> AddBCC ('bcc @ example.com ');
$ Mail-> WordWrap = 50; // Set word wrap to 50 characters
$ Mail-> AddAttachment ('/var/tmp/file.tar.gz'); // Add attachments
$ Mail-> AddAttachment ('/tmp/image.jpg', 'new.jpg '); // Optional name
$ Mail-> IsHTML (true); // Set email format to HTML
$ Mail-> Subject = 'here is the subobject ';
$ Mail-> Body = 'This is the HTML message bodyIn bold!';
$ Mail-> AltBody = 'This is the body in plain text for non-HTML mail clients ';
If (! $ Mail-> Send ()){
Echo 'message cocould not be sent .';
Echo 'mailer Error: '. $ mail-> ErrorInfo;
Exit;
}
Echo 'Message has been sent ';
?>
The solution is simple. add a property in the code:
$ Mail-> CharSet = "UTF-8"; // sets the character set encoding.
If it is gb2312 encoding, it is:
$ Mail-> CharSet = "GB2312 ";
Save the file to the corresponding encoding format.