Example of sending an email using the pear: Net_SMTP class

Source: Internet
Author: User
Example of sending an email using the pear: Net_SMTP class

  1. Require 'net/SMTP. php ';

  2. $ Host = '1970. com'; // ip address or domain name of the smtp server

  3. $ Username = 'arcow'; // username used to log on to the smtp server
  4. $ Password = 'secret'; // password used to log on to the smtp server
  5. $ From = 'jbxue @ 126.com '; // who sent the email
  6. $ Rcpt = array ('test @ test.com ', 'jbxue @ 126.com'); // multiple receivers can be set.
  7. $ Subj = "Subject: Who are you \ n"; // Subject
  8. $ Body = "test it"; // email content

  9. /* Create a class */

  10. If (! ($ Smtp = new Net_SMTP ($ host ))){
  11. Die ("cannot initialize class Net_SMTP! \ N ");
  12. }

  13. /* Start to connect to the SMTP server */

  14. If (PEAR: isError ($ e = $ smtp-> connect ())){
  15. Die ($ e-> getMessage (). "\ n ");
  16. }

  17. /* Smtp Authentication Required */

  18. $ Smtp-> auth ($ username, $ password, "PLAIN ");

  19. /* Set the sender's email address */

  20. If (PEAR: isError ($ smtp-> mailFrom ($ from ))){
  21. Die ("the sender's email address cannot be set to <$ from> \ n ");
  22. }

  23. /* Set the recipient */

  24. Foreach ($ rcpt as $ ){
  25. If (PEAR: isError ($ res = $ smtp-> rcp1_( $ ))){
  26. Die ("the email cannot be delivered to <$ to>:". $ res-> getMessage (). "\ n ");
  27. }
  28. }

  29. /* Start sending the email content */

  30. If (PEAR: isError ($ smtp-> data ($ subj. "\ r \ n". $ body ))){
  31. Die ("Unable to send data \ n ");
  32. }

  33. /* Disconnect */

  34. $ Smtp-> disconnect ();
  35. Echo "sent successfully! ";
  36. ?>

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.