当前位置:Gxlcms > PHP教程 > PHP使用SMTP发送邮件(PEAR)

PHP使用SMTP发送邮件(PEAR)

时间:2021-07-01 10:21:17 帮助过:24人阅读

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());}

人气教程排行