欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP 使用 SMTP 发送邮件(PEAR)

程序员文章站 2022-06-12 18:50:34
...
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());}
复制代码