Home > Backend Development > PHP Tutorial > Use PHPMailer to send emails with attachments and support HTML content

Use PHPMailer to send emails with attachments and support HTML content

WBOY
Release: 2016-07-25 08:44:15
Original
896 people have browsed it
PHPMailer is an encapsulated PHP mail sending class that supports sending emails with HTML content and adding attachments for sending. Unlike PHP itself, the mail() function does not require server environment support. You only need to set up the mail server with relevant information. Implement the email sending function.

This article will use example code to explain how to set up and implement the function of sending HTML and emails with attachments.

First, you can go to PHPMailer obtains the latest download package and extracts it to the WEB directory.

Then create a sendmail.php file, load the PHPMailer class, and set relevant attribute parameters, such as email server address, sender and recipient, email content, etc. Please see the code for details:

  1. require_once('class.phpmailer.php'); //Load the PHPMailer class
  2. $mail = new PHPMailer(); //Instantiate
  3. $mail->IsSMTP(); // Enable SMTP
  4. $mail->Host = "smtp.163.com"; //The SMTP server takes 163 mailbox as an example
  5. $mail->Port = 25; //Mail sending port
  6. $mail->SMTPAuth = true; //Enable SMTP authentication
  7. $mail->CharSet = "UTF-8"; //Character set
  8. $mail->Encoding = "base64"; //Encoding method
  9. $mail->Username = " helloweba@163.com"; //Your email
  10. $mail->Password = "xxx"; //Your password
  11. $mail->Subject = "Hello"; //Email title
  12. $mail ->From = "helloweba@163.com"; //Sender's address (that is, your email)
  13. $mail->FromName = "Moonlight"; //Sender's name
  14. $address = "xyz@163.com";//Recipient email
  15. $mail->AddAddress($address, "Dear");//Add recipient (address, nickname)
  16. $mail->AddAttachment( 'xx.xls','My attachments.xls'); //Add attachments and specify the name
  17. $mail->IsHTML(true); //Support html format content
  18. $mail->AddEmbeddedImage("logo .jpg", "my-attach", "logo.jpg"); //Set the picture in the email
  19. $mail->Body = 'Hello, Friend!
    This is an email from target="_blank">helloweba.com!
  20. helloweba'; //Email body content
  21. //Send
  22. if(!$mail->Send() ) {
  23. echo "Mailer Error: " . $mail->ErrorInfo;
  24. } else {
  25. echo "Message sent!";
  26. }
Copy code

As can be seen from the code, after instantiating PHPMailer, we specify to use SMTP to send emails, set up the SMTP mail server, and enable SMTP authentication. If your mail server does not require authentication, set $mail->SMTPAuth=false , and can be sent without a password. Then set the character set and encoding to support Chinese characters. Note that the original PHPMailer package does not have ideal support for Chinese characters, so you can download the improved package in the helloweba example. Then set the sender and recipient, and add attachments. Note that it is best not to use Chinese for the original name of the attachment. You can specify the Chinese name in AddAttachment(). Then set the html content of the email, and finally send it. The process is clear at a glance.

PHPMailer, HTML


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