Use Phpmailer to send messages with attachments and support HTML content

Source: Internet
Author: User
Phpmailer is a packaged PHP mail sending class, support for sending HTML content of e-mail, and can add attachments to send, not like the php itself mail () function requires server environment support, you only need to set up the mail server with relevant information to achieve the mail delivery function.

This article will combine the example code to explain how to set up and implement the sending HTML and the mail with attachments.

First, you can go to Phpmailer to get the latest download package and extract it to 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., please see the code:

  1. Require_once (' class.phpmailer.php '); Load Phpmailer class
  2. $mail = new Phpmailer (); Instantiation of
  3. $mail->issmtp (); Enable SMTP
  4. $mail->host = "smtp.163.com"; SMTP server with 163 mailboxes as an example
  5. $mail->port = 25; Mail Send port
  6. $mail->smtpauth = true; Enable SMTP authentication
  7. $mail->charset = "UTF-8"; Character
  8. $mail->encoding = "base64"; Encoding method
  9. $mail->username = "helloweba@163.com"; Your e-mail
  10. $mail->password = "xxx"; Your password.
  11. $mail->subject = "Hello"; Message header
  12. $mail->from = "helloweba@163.com"; Sender address (that is, your mailbox)
  13. $mail->fromname = "Moonlight Light"; Sender's name
  14. $address = "xyz@163.com";//Recipient Email
  15. $mail->addaddress ($address, "Pro");//Add Recipient (address, nickname)
  16. $mail->addattachment (' Xx.xls ', ' my attachments. xls '); Add an attachment and specify a name
  17. $mail->ishtml (TRUE); Support HTML format content
  18. $mail->addembeddedimage ("logo.jpg", "My-attach", "logo.jpg"); Set up a picture in a message
  19. $mail->body = ' Hello, friend !
    This is an e-mail from target= "_blank" >helloweba.com!
  20. '; Message 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 that SMTP is used to send mail, set up an SMTP mail server, and enable SMTP authentication, if your mail server does not require authentication, set $mail->smtpauth=false, And you don't need a password to send it. Then set the character set and encoding support Chinese characters, note that the original Phpmailer package has less than ideal support for Chinese characters, so you can download the improvements package in the Helloweba sample. Then set the sender and recipient to add attachments. Note the attachment is best not in Chinese, you can specify the Chinese name in AddAttachment (). Then set the message HTML content, the last is sent, the process at a glance,

Phpmailer, HTML
  • Related Article

    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.