Home > Backend Development > PHP Tutorial > About the usage of phpmailer

About the usage of phpmailer

WBOY
Release: 2016-07-25 09:07:13
Original
1146 people have browsed it
  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 = "200.162.244.66"; // SMTP servers
  6. $mail->SMTPAuth = true; // turn on SMTP authentication
  7. $mail->Username = "yourmail"; // SMTP username Note: Ordinary email authentication does not require adding @domain name
  8. $mail->Password = "mailPassword"; // SMTP password
  9. $mail->From = "yourmail@yourdomain.com"; // Sender's email
  10. $mail->FromName = "Administrator"; // Sender
  11. $mail->CharSet = "GB2312"; // Specify here character set!
  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
  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 = "
  23. I love php
  24. ";
  25. $mail->AltBody ="text/html";
  26. if(!$mail->Send())
  27. {
  28. echo "Send email Error

    ";

  29. echo "Mail error message: " . $mail->ErrorInfo;
  30. exit;
  31. }
  32. else {
  33. echo "$user_name Email sent successfully!
    ";
  34. }
  35. }
  36. // Parameter description (send to, email subject, email content, additional information, user name)
  37. smtp_mail("yourmail@yourdomain.com", "Welcome to phpmailer! ", "NULL", "yourdomain.com", "username");
  38. ?>
Copy code

Note: 1. Email character set setting, $mail->CharSet = "GB2312"; // Specify the character set here! Here I only specify GB2312 because this way Outlook can display the email subject normally. I have tried setting it to utf-8 but it displays garbled characters in Outlook. 2. If you are sending emails in html format, remember to also specify 3. If you want to use it to send mass emails, remember to modify the include file function, such as: require("phpmailer/class.phpmailer.php"); Change to require_once("phpmailer/class.phpmailer.php"); Otherwise, class redefinition will occur.

Personally, I think that to use phpmailer, first of all, you need to have a mail server. PHP's mail function is not specified. It should be the SMTP set by PHP. Here you need to specify it specifically, and you also need to specify the administrator and password of the mail server.



Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template