Home >Backend Development >PHP Tutorial >PHPmailer sends emails and solves the problem of garbled characters

PHPmailer sends emails and solves the problem of garbled characters

WBOY
WBOYOriginal
2016-07-25 08:59:37992browse
  1. phpmailer email sending test-bbs.it-home.org

  2. Please enter Email address to receive:
  3. Email address:

Copy code

2. Email program send.php

  1. /**

  2. * PHPMailer email sending
  3. * Edit bbs.it-home.org
  4. */
  5. require("class.phpmailer.php");
  6. $mail = new PHPMailer();
  7. $mail-> ;CharSet = "gb2312"; // Specify the character set here! If it is utf-8, change gb2312 to utf-8
  8. $mail->Encoding = "base64";
  9. $address = $_POST['address'];
  10. $mail->IsSMTP(); // set mailer to use SMTP
  11. $mail->Host = "smtp.126.com"; // specify main and backup server
  12. $mail->SMTPAuth = true; // turn on SMTP authentication
  13. $mail->Username = ""; // SMTP username
  14. $mail->Password = "******"; // SMTP password

  15. $mail->From = "";

  16. $ mail->FromName = "rokaye";
  17. $mail->AddAddress("$address", "");
  18. //$mail->AddAddress(""); // name is optional
  19. //$ mail->AddReplyTo("", "");

  20. //$mail->WordWrap = 50; // set word wrap to 50 characters

  21. //$mail-> AddAttachment("/var/tmp/file.tar.gz"); // add attachments
  22. //$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // optional name
  23. //$mail->IsHTML(true); // set email format to HTML

  24. $mail->Subject = "PHPMailer test email";

  25. $mail->Body = "Hello, this is rokaye's test email";
  26. $mail->AltBody = "This is the body in plain text for non-HTML mail clients";

  27. if(!$mail ->Send())

  28. {
  29. echo "Message could not be sent.

    ";

  30. echo "Mailer Error: " . $mail->ErrorInfo;
  31. exit;
  32. }

  33. echo "Message has been sent";

  34. ?>

Copy code

Problems with garbled characters are mostly caused by not specifying the required encoding.

  1. $mail->CharSet = "gb2312"; // Specify the character set! If it is utf-8, change gb2312 to utf-8
  2. $mail->Encoding = "base64";
Copy the code

After doing this, there will be no garbled characters.



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