Home  >  Article  >  Backend Development  >  Example of sending HTML content and emails with attachments using PHPMailer_PHP Tutorial

Example of sending HTML content and emails with attachments using PHPMailer_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:26:31815browse

PHPMailer is an encapsulated PHP mail sending class that supports sending emails with HTML content and can add attachments for sending. Unlike PHP itself, the mail() function does not require server environment support. You only need to set up the mail server to relevant Information can be used to send emails.
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 get the latest download package from PHPMailer and unzip 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. For details, please see the code:

Copy code The code is as follows:

require_once('class.phpmailer.php'); //Load the PHPMailer class

$mail = new PHPMailer(); //Instantiation
$mail->IsSMTP(); // Enable SMTP
$mail->Host = "smtp.163.com"; //SMTP server, taking 163 mailbox 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 email
$mail->Password = "xxx"; //Your password
$mail->Subject = "Hello"; //Email title

$mail->From = "helloweba@163.com"; //Sender address (that is, your email address)
$mail->FromName = "月光光"; //Sender's name

$address = "xyz@163.com";//recipient email
$mail->AddAddress($address, "Dear");//Add recipient (address, nickname)

$mail->AddAttachment('xx.xls','My Attachment.xls'); // Add attachment and specify name
$mail->IsHTML(true); //Support html format content
$mail->AddEmbeddedImage("logo.jpg", "my-attach", "logo.jpg"); //Set the image in the email
$mail->Body = 'Hello, Friend!
This is an email from target="_blank">jb51.net's email!

helloweba'; //Email body content

//Send
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}

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 recipients, 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,
If sent successfully, you will receive the following email:

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824691.htmlTechArticlePHPMailer is a packaged PHP email sending class that supports sending emails with HTML content and can add attachments for sending. , unlike PHP itself, the mail() function requires server environment support...
Statement:
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