PHPMailer sends emails with attachments and HTML content

Source: Internet
Author: User
Various official and user-released code examples are provided. For code reference, you are welcome to learn PHPMailer is an encapsulated PHP mail sending class that supports sending HTML content emails, you can also add attachments for sending. Unlike the mail () function of PHP itself, which 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: 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"> helloweba.com!

'; // 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,

: Http://www.shouce.ren/post/d/id/176

More:
The most detailed phpmailer usage
Http://www.shouce.ren/post/view/id/15
Thinkphp 3.2.2 integrate PHPMailer mail plug-in
Http://www.shouce.ren/post/view/id/1554
Thinkphp 3.2.3 integrate PHPMailer mail plug-in
Http://www.shouce.ren/post/view/id/1632
ThinkPHP email sending Function
Http://www.shouce.ren/post/view/id/2777

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.