This article mainly introduces PHPMailer's example of sending HTML content and emails with attachments. The sent content contains pictures and text, and an EXCEL table sent by attachments. For more information, see
This article mainly introduces PHPMailer's example of sending HTML content and emails with attachments. The sent content contains pictures and text, and an EXCEL table sent by attachments. For more information, see
PHPMailer is an encapsulated PHP mail sending class that supports sending HTML content emails and adding attachments for sending. It is not like the mail () function of PHP itself that requires support from the server environment, you only need to set up the mail server to send emails with relevant information.
This document describes how to configure and implement the function of sending HTML and emails with attachments based on the instance code.
First, you can go to PHPMailer to obtain the latest download package and decompress it to the WEB directory.
Create a sendmail. php file, load the PHPMailer class, and set relevant attribute parameters, such as the mail server address, sender and recipient, and mail content. For details, see the code:
The Code is as follows:
Require_once ('class. phpmailer. php'); // load the PHPMailer class
$ Mail = new PHPMailer (); // instantiate
$ Mail-> IsSMTP (); // enable SMTP
$ Mail-> Host = "smtp.163.com"; // The SMTP server uses the 163 email address as an example.
$ Mail-> Port = 25; // mail sending Port
$ Mail-> SMTPAuth = true; // enable SMTP authentication
$ Mail-> CharSet = "UTF-8"; // Character Set
$ Mail-> Encoding = "base64"; // Encoding method
$ Mail-> Username = "helloweba@163.com"; // your mailbox
$ Mail-> Password = "xxx"; // your Password
$ Mail-> Subject = "hello"; // mail title
$ Mail-> From = "helloweba@163.com"; // sender address (that is, your mailbox)
$ Mail-> FromName = "Zhuguang"; // sender's name
$ Address = "xyz@163.com"; // recipient email
$ Mail-> AddAddress ($ address, ""); // Add a recipient (address, nickname)
$ Mail-> addattachment('xx.xls', 'My attachment .xls '); // Add the attachment and specify the name
$ Mail-> IsHTML (true); // html content is supported
$ Mail-> AddEmbeddedImage ("logo.jpg", "my-attach", "logo.jpg"); // you can specify an image in the email.
$ Mail-> Body = 'hello,Friend!
This is an email from target = "_ blank"> jb51.net!
'; // Subject content
// Send
If (! $ Mail-> Send ()){
Echo "Mailer Error:". $ mail-> ErrorInfo;
} Else {
Echo "Message sent! ";
}
As you can see from the Code, after instantiating PHPMailer, we specify to use SMTP to send emails, set the SMTP mail server, and enable SMTP authentication. If your mail server does not require authentication, set $ mail-> SMTPAuth to false, and send messages without a password. Then set the character set and encoding to support Chinese characters. Note that the PHPMailer package in the original version does not support Chinese characters very well. Therefore, you can download the improved package in the helloweba example. Set the sender and recipient, and add the attachment. Note that the original name of the attachment should not be in Chinese. You can specify a Chinese name in AddAttachment. Set the html content of the email, and send the email at a glance,
If the email is successfully sent, you will receive the following email: