Use the open-source project PHPMailer to send emails

Source: Internet
Author: User
Use the open-source project PHPMailer to send emails
This is to use the open-source project PHPMailer to implement mail sending. first download the file. here I download the version 5.1, and then put the three classes. ***. put the php file under the project file. I first used gmail, but failed. The problem was that the authentication failed. later I used QQ mail and sent it successfully.

  1. Require ("class. phpmailer. php"); // the downloaded file must be in the directory where the file is located.
  2. $ Mail = new PHPMailer (); // Create a mail sending class
  3. $ Address = "youbinliu@126.com ";
  4. $ Mail-> IsSMTP (); // send using SMTP
  5. $ Mail-> Host = "smtp.qq.com"; // your enterprise Post Office domain name
  6. $ Mail-> SMTPAuth = true; // enable SMTP verification
  7. $ Mail-> Username = "843831601@qq.com"; // Post Office Username (enter the complete email address)
  8. $ Mail-> Password = "************"; // Post Office Password
  9. $ Mail-> Port = 25;
  10. $ Mail-> From = "843831601@qq.com"; // email address of the sender
  11. $ Mail-> FromName = "liuyoubin ";
  12. $ Mail-> AddAddress ("$ address", "a"); // The recipient address, which can be replaced with any email address to receive the email, the format is AddAddress ("recipient email", "recipient name ")
  13. // $ Mail-> AddReplyTo ("","");
  14. // $ Mail-> AddAttachment ("/var/tmp/file.tar.gz"); // add an attachment
  15. // $ Mail-> IsHTML (true); // set email format to HTML // whether HTML format is used
  16. $ Mail-> Subject = "PHPMailer test mail"; // mail title
  17. $ Mail-> Body = "Hello, this is the test mail"; // The mail content
  18. $ Mail-> AltBody = "This is the body in plain text for non-HTML mail clients"; // additional information, which can be omitted
  19. If (! $ Mail-> Send ())
  20. {
  21. Echo "failed to send the email.

    ";

  22. Echo "error cause:". $ mail-> ErrorInfo;
  23. Exit;
  24. }
  25. Echo "email sent successfully ";
  26. /*************************************** **********
  27. Attachment:
  28. Phpmailer (simplified version)
  29. Start with:
  30. $ AltBody -- attribute
  31. From: PHPMailer ::$ AltBody
  32. File: class. phpmailer. php
  33. Note: the setting of this attribute is that HTML standby display is not supported in the mail body.
  34. AddAddress -- method
  35. From: PHPMailer: AddAddress (), File: class. phpmailer. php
  36. Note: Add recipients. Parameter 1 is the recipient's mailbox, and parameter 2 is the recipient's name. Example AddAddress ("eb163@eb163.com", "eb163"), but parameter 2 is optional, AddAddress (eb163@eb163.com) is also possible.
  37. Function prototype: public function AddAddress ($ address, $ name = ''){}
  38. AddAttachment -- method
  39. From: PHPMailer: AddAttachment ()
  40. File: class. phpmailer. php.
  41. Note: add attachments.
  42. Parameter: Path, name, encoding, type. The path is required, and the others are optional.
  43. Function prototype:
  44. AddAttachment ($ path, $ name = '', $ encoding = 'base64', $ type = 'application/octet-stream '){}
  45. AddBCC -- method
  46. From: PHPMailer: AddBCC ()
  47. File: class. phpmailer. php
  48. Note: add a BCC. For the differences between CC and BCC, see [differences between BCC and CC in SMTP sending].
  49. Parameter 1 is the address and parameter 2 is the name. Note that this method only supports SMTP in win32 and does not support the mail function.
  50. Function prototype: public function AddBCC ($ address, $ name = ''){}
  51. AddCC -- method
  52. From: PHPMailer: AddCC ()
  53. File: class. phpmailer. php
  54. Note: add a CC. For the differences between CC and BCC, see [differences between BCC and CC in SMTP sending].
  55. Parameter 1 is the address and parameter 2 is the name. Note that this method only supports SMTP in win32 and does not support the mail function.
  56. Function prototype: public function AddCC ($ address, $ name = ''){}
  57. AddCustomHeader -- method
  58. From: PHPMailer: AddCustomHeader ()
  59. File: class. phpmailer. php
  60. Note: add a custom email header.
  61. The parameter is the header information.
  62. Function prototype: public function AddCustomHeader ($ custom_header ){}
  63. AddEmbeddedImage -- method
  64. From: PHPMailer: AddEmbeddedImage ()
  65. File: class. phpmailer. php
  66. Add an embedded image.
  67. Parameter: Path, return handle [, name, encoding, type]
  68. Function prototype: public function AddEmbeddedImage ($ path, $ cid, $ name = '', $ encoding = 'base64', $ type = 'application/octet-stream '){}
  69. Tip: AddEmbeddedImage (PICTURE_PATH. "index_01.jpg", "img_01", "index_01.jpg ");
  70. Reference in html
  71. AddReplyTo -- method
  72. From: PHPMailer: AddRepl
  73. **************************************** *********/
  74. ?>


Open Source, email sending, PHPMailer

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.