Use PHPMailer to send emails (HTML content, images, and attachments can be sent)

Source: Internet
Author: User
Phpmailer is used to send emails (html content, images, and attachments can be sent). phpmailer is a php class used to send emails. it is much better than the built-in function mail in php. phpmailer

Phpmailer is used to send emails (html content, images, and attachments can be sent). phpmailer is a php class used to send emails. it is much better than the built-in function mail in php, phpmailer can be downloaded from the official website.

Here is an example of sending only text:

  1. */
  2. Require ("class. phpmailer. php ");
  3. $ Mail = new phpmailer ();
  4. $ Mail-> ismail ();
  5. $ Mail-> addaddress ("email@example.com ");
  6. $ Mail-> subject = "test 1 ";
  7. $ Mail-> body = "test 1 of phpmailer .";
  8. If (! $ Mail-> send ())
  9. {
  10. Echo "error sending:". $ mail-> errorinfo ;;
  11. }
  12. Else
  13. {
  14. Echo "letter sent ";
  15. }
  16. /*
  17. $ Mail-> ismail (); required
  18. Issendmail-via sendmail command.
  19. Isqmail-directly via qmail mta.
  20. Issmtp-via smtp server.

Here is an smtp sample, we assume that this smtp requires authorization. if in't nessesary, only write $ email> smtpauth = 0;, the number of servers to be used uses semicolumn as the separator.

  1. */
  2. Require ("class. phpmailer. php ");
  3. $ Mail = new phpmailer ();
  4. $ Mail-> issmtp ();
  5. $ Mail-> host = "smtp1.example.com; smtp2.example.com ";
  6. $ Mail-> smtpauth = true;
  7. $ Mail-> username = 'smtpusername ';
  8. $ Mail-> password = 'smtppassword ';
  9. $ Mail-> addaddress ("email@example.com ");
  10. $ Mail-> subject = "test 1 ";
  11. $ Mail-> body = "test 1 of phpmailer .";
  12. If (! $ Mail-> send ())
  13. {
  14. Echo "error sending:". $ mail-> errorinfo ;;
  15. }
  16. Else
  17. {
  18. Echo "letter is sent ";
  19. }
  20. /*

To add sender inforation, use the following features:

  1. Mail-> from = "mailer@example.com ";
  2. $ Mail-> fromname = "my site's mailer ";
  3. $ Mail-> sender = "mailer@example.com"; // indicates returnpath header
  4. $ Mail-> addreplyto ("replies@example.com", "replies for my site"); // indicates replyto headers
  5. For specifying various types of specified ients use these:
  6. $ Mail-> addaddress ("mail1@domain.com", "your ient 1 ");
  7. $ Mail-> addcc ("mail1@domain.com", "your ient 1 ");
  8. $ Mail-> addbcc ("mail1@domain.com", "your ient 1 ");

How to use garbled characters

$ Mail-> charset = "windows-1251"; $ mail-> charset = "UTF-8 ";

Set the encoding. if you want to send an email, you can send an image and add the following code in front of $ mail-send:

  1. $ Mail-> ishtml (true );
  2. $ Mail-> addembeddedimage('logo.jpg ', 'logomg', 'logo.jpg '); // attach file logo.jpg, and later link to it using identfier logoimg
  3. $ Mail-> body = "test 1 of phpmailer html
  4. This is a test picture: "cid: logoimg"/>

    ";
  5. $ Mail-> altbody = "this is text only alternative body .";

Send attachment

  1. $ Mail-> ishtml (false );
  2. $ Mail-> addattachment ('www .phpfensi.com/invoice-user-1234.pdf', 'invoice.pdf '); // attach files/invoice-user-1234.pdf,
  3. */

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.