Home > Backend Development > PHP Tutorial > Example of sending email using pear:Net_SMTP class

Example of sending email using pear:Net_SMTP class

WBOY
Release: 2016-07-25 09:11:07
Original
1235 people have browsed it
  1. require 'Net/SMTP.php';

  2. $host = '126.com';//smtp server ip or Domain name

  3. $username= 'arcow';//Username to log in to the smtp server
  4. $password= 'secret';//Password to log in 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";//Subject
  8. $body = "test it";//Email content

  9. /* Create a class*/

  10. if (! ($smtp = new Net_SMTP($host))) {
  11. die("Unable to initialize Class Net_SMTP!n");
  12. }

  13. /* Start connecting to SMTP server*/

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

  17. /* smtp requires authentication*/

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

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

  20. if (PEAR::isError($smtp->mailFrom($from))) {
  21. die("Cannot set The sender's email address is<$from>n");
  22. }

  23. /* Set the recipient of the email*/

  24. foreach ($rcpt as $to) {
  25. if (PEAR::isError ($res = $smtp->rcptTo($to))) {
  26. die("Mail cannot be delivered to <$to>: " . $res->getMessage() . "n");
  27. }
  28. }

  29. /* Start sending email content*/

  30. if (PEAR::isError($smtp->data($subj . "rn" . $body))) {
  31. die( "Unable to send datan");
  32. }

  33. /* Disconnect*/

  34. $smtp->disconnect();
  35. echo "Send successfully! ";
  36. ?>

Copy code



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template