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

Jul 25, 2016 am 08:59 AM

  1. <html>
  2. <body>
  3. <h3>phpmailer email sending test-bbs.it-home.org</h3>
  4. Please enter <font color="#FF6666"> Email address to receive</font>:
  5. <form name="phpmailer" action="send.php" method="post">
  6. <input type="hidden" name="submitted" value= "1"/>
  7. Email address: <input type="text" size="50" name="address" />
  8. <br/>
  9. <input type="submit" value=" Send "/>
  10. </form>
  11. </body>
  12. </html>
Copy code

2. Email program send.php

  1. <p><?php
  2. /**
  3. * PHPMailer email sending
  4. * Edit bbs.it-home.org
  5. */
  6. require("class.phpmailer.php");
  7. $mail = new PHPMailer();
  8. $mail-> ;CharSet = "gb2312"; // Specify the character set here! If it is utf-8, change gb2312 to utf-8
  9. $mail->Encoding = "base64";
  10. $address = $_POST['address'];
  11. $mail->IsSMTP(); // set mailer to use SMTP
  12. $mail->Host = "smtp.126.com"; // specify main and backup server
  13. $mail->SMTPAuth = true; // turn on SMTP authentication
  14. $mail->Username = ""; // SMTP username
  15. $mail->Password = "******"; // SMTP password</p>
  16. <p>$mail->From = "";
  17. $ mail->FromName = "rokaye";
  18. $mail->AddAddress("$address", "");
  19. //$mail->AddAddress(""); // name is optional
  20. //$ mail->AddReplyTo("", "");</p>
  21. <p>//$mail->WordWrap = 50; // set word wrap to 50 characters
  22. //$mail-> AddAttachment("/var/tmp/file.tar.gz"); // add attachments
  23. //$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // optional name
  24. //$mail->IsHTML(true); // set email format to HTML</p>
  25. <p>$mail->Subject = "PHPMailer test email";
  26. $mail->Body = "Hello, this is rokaye's test email";
  27. $mail->AltBody = "This is the body in plain text for non-HTML mail clients";</p>
  28. <p>if(!$mail ->Send())
  29. {
  30. echo "Message could not be sent. <p>";
  31. echo "Mailer Error: " . $mail->ErrorInfo;
  32. exit;
  33. }</p>
  34. <p>echo "Message has been sent";
  35. ?></p>
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 of this Website
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

Hot Article