首页 >后端开发 >php教程 >PHP 使用 SMTP 发送邮件(PEAR)

PHP 使用 SMTP 发送邮件(PEAR)

WBOY
WBOY原创
2016-07-25 09:09:15861浏览
PHPer 多数使用 mail 函数来发送邮件,但我们可以使用其他的 SMTP 服务器来发送,这里推荐使用 PEAR’s mail package 来发送邮件。
  1. $subject = "This mail is sent from SMTP.";
  2. $mail_body = "This is the body of the mail which is sent using SMTP.";
  3. $from = "From: From Name ";
  4. $to = "To: To Name ";
  5. $receiver = "toaddress@xpertdeveloper.com";
  6. // Setting up the headers
  7. $headers["From"] = $from;
  8. $headers["To"] = $to;
  9. $headers["Subject"] = $subject;
  10. $headers["Reply-To"] = "reply@address.com";
  11. $headers["Content-Type"] = "text/plain; charset=ISO-2022-JP";
  12. $headers["Return-path"] = "returnpath@address.com";
  13. // Setting up the SMTP setting
  14. $smtp_info["host"] = "smtp.server.com";
  15. $smtp_info["port"] = "25";
  16. $smtp_info["auth"] = true;
  17. $smtp_info["username"] = "smtp_user";
  18. $smtp_info["password"] = "smtp_password";
  19. // Creating the PEAR mail object :
  20. $mail_obj =& Mail::factory("smtp", $smtp_info);
  21. // Sending the mail now
  22. $mail_sent = $mail_obj->send($receiver, $headers, $mail_body);
  23. // If any error the see for that here:
  24. if (PEAR::isError($mail_sent)) { print($mail_sent->getMessage());}
复制代码


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn