Phpmailer Send HTML content, _php instance with attachment message instance

Source: Internet
Author: User

Phpmailer is a packaged PHP mail sending class that supports sending HTML content emails, and can add attachments to send, unlike PHP itself mail () function requires server environment support, you only need to set up the mail server to the relevant information to achieve the message delivery function.
This article will combine the instance code to explain how to set up and implement the send HTML and the message features with attachments.

First, you can go to Phpmailer to get the latest download package and extract it into the Web directory.
Then create a sendmail.php file, load the Phpmailer class, and set the relevant property parameters, such as mail server address, sender and recipient, message content, etc., see Code:

Copy Code code as follows:

Require_once (' class.phpmailer.php '); Load Phpmailer class

$mail = new Phpmailer (); Instantiation of
$mail->issmtp (); Enable SMTP
$mail->host = "smtp.163.com"; SMTP server with 163 mailboxes as an example
$mail->port = 25; Mail Send port
$mail->smtpauth = true; Enable SMTP authentication

$mail->charset = "UTF-8"; Character
$mail->encoding = "base64"; Encoding method

$mail->username = "helloweba@163.com"; Your e-mail
$mail->password = "xxx"; Your code.
$mail->subject = "Hello"; Message headers

$mail->from = "helloweba@163.com"; Sender address (that is, your mailbox)
$mail->fromname = "Moonlight Light"; Sender Name

$address = "xyz@163.com";//Recipient Email
$mail->addaddress ($address, "Pro");//Add Recipient (address, nickname)

$mail->addattachment (' Xx.xls ', ' my attachment. xls '); Add an attachment and specify a name
$mail->ishtml (TRUE); Support HTML format content
$mail->addembeddedimage ("logo.jpg", "My-attach", "logo.jpg"); Set a picture in a message
$mail->body = ' Hello, <b> friend </b>! <br/> This is a letter from <a href= "Http://www.jb51.net"
target= "_blank" >jb51.net</a> mail! <br/>
; Message body 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 mail, set up SMTP mail server, and enable SMTP authentication, set $mail->smtpauth=false if your mail server does not require authentication. And it can be sent without a password. Then set the character set and encoding to support Chinese characters, note that the original Phpmailer package is not ideal for Chinese character support, so you can download the improvement package in the Helloweba example. Then set the sender and recipient to add the attachment. Note that the name of the attachment is best not in Chinese, you can specify the AddAttachment () in Chinese. Then set the message HTML content, and finally send, flow at a glance,
If sent successfully, you will receive the following message:

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.