Home  >  Article  >  Backend Development  >  Example of php using phpMailer to send emails

Example of php using phpMailer to send emails

WBOY
WBOYOriginal
2016-07-25 09:03:081154browse
  1. require("phpmailer/class.phpmailer.php");
  2. function smtp_mail( $sendto_email, $subject, $body, $extra_hdrs, $user_name){
  3. $mail = new PHPMailer( );
  4. $mail->IsSMTP(); // send via SMTP
  5. $mail->Host = "smtp.163.com"; // SMTP servers
  6. $mail->SMTPAuth = true; // turn on SMTP authentication
  7. $mail->Username = "xuchao842363331"; // SMTP username Note: Ordinary email authentication does not require adding @domain name. This is my 163 email address
  8. $mail->Password = "password"; // SMTP password Enter the email password here
  9. $mail->From = "jbxue123@163.com"; // Sender's email address
  10. $mail->FromName = "Administrator"; // Sender
  11. $ mail->CharSet = "UTF-8"; // Specify the character set here! After specifying UTF-8, the title and sender of the email will not be garbled. If it is GB2312, the title will be garbled.
  12. $mail->Encoding = "base64";
  13. $mail->AddAddress($sendto_email,"username" ); // Recipient email and name
  14. $mail->AddReplyTo("yourmail@yourdomain.com","yourdomain.com");
  15. //$mail->WordWrap = 50; // set word wrap Number of newline characters
  16. //$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment attachment
  17. //$mail->AddAttachment("/tmp/image.jpg", " new.jpg");
  18. //$mail->IsHTML(true); // send as HTML
  19. // Email subject
  20. $mail->Subject = $subject;
  21. // Email content
  22. $mail-> ;Body = "hello! PHPMailer";
  23. //$mail->AltBody ="text/html";
  24. if(!$mail->Send())
  25. {
  26. echo "error

    ";

  27. echo "error: " . $mail->ErrorInfo;
  28. exit;
  29. }
  30. else {
  31. echo "success!";
  32. }
  33. }
  34. // Parameter description (send to, email subject, email content, additional information , username)
  35. ?>
Copy code

Note: When the character set is specified as GB2312, the title will be garbled. If it is specified as UTF-8 here, it will not be garbled. In fact, PHPMailer also has many functions, such as adding attachments, etc., which will not be demonstrated here.

In this way, you can call this function when you need to use the email function:

  1. require("mail.php");
  2. smtp_mail("790896688@qq.com", "Welcome to Programmer's Home", "", "", "username") ;
  3. ?>
Copy code


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