Home Backend Development PHP Tutorial About the usage of phpmailer

About the usage of phpmailer

Jul 25, 2016 am 09:07 AM

  1. <?php
  2. require("phpmailer/class.phpmailer.php");
  3. function smtp_mail( $sendto_email, $subject, $body, $extra_hdrs, $user_name){
  4. $mail = new PHPMailer( );
  5. $mail->IsSMTP(); // send via SMTP
  6. $mail->Host = "200.162.244.66"; // SMTP servers
  7. $mail->SMTPAuth = true; // turn on SMTP authentication
  8. $mail->Username = "yourmail"; // SMTP username Note: Ordinary email authentication does not require adding @domain name
  9. $mail->Password = "mailPassword"; // SMTP password
  10. $mail->From = "yourmail@yourdomain.com"; // Sender's email
  11. $mail->FromName = "Administrator"; // Sender
  12. $mail->CharSet = "GB2312"; // Specify here character set!
  13. $mail->Encoding = "base64";
  14. $mail->AddAddress($sendto_email,"username"); // Recipient email and name
  15. $mail->AddReplyTo("yourmail@yourdomain.com ","yourdomain.com");
  16. //$mail->WordWrap = 50; // set word wrap
  17. //$mail->AddAttachment("/var/tmp/file.tar.gz" ); // attachment attachment
  18. //$mail->AddAttachment("/tmp/image.jpg", "new.jpg");
  19. $mail->IsHTML(true); // send as HTML
  20. / / Email subject
  21. $mail->Subject = $subject;
  22. // Email content
  23. $mail->Body = "
  24. <html><head>
  25. <meta http-equiv="Content-Language" content="zh-cn">
  26. <meta http-equiv="Content-Type" content="text/html; charset=GB2312">
  27. </head>
  28. <body>
  29. I love php
  30. </body>
  31. </html>
  32. ";
  33. $mail->AltBody ="text/html";
  34. if(!$mail->Send())
  35. {
  36. echo "Send email Error<p>";
  37. echo "Mail error message: " . $mail->ErrorInfo;
  38. exit;
  39. }
  40. else {
  41. echo "$user_name Email sent successfully!<br />";
  42. }
  43. }
  44. // Parameter description (send to, email subject, email content, additional information, user name)
  45. smtp_mail("yourmail@yourdomain.com", "Welcome to phpmailer! ", "NULL", "yourdomain.com", "username");
  46. ?>
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.



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 Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles