首頁 >後端開發 >php教程 >PHP 使用 SMTP 傳送郵件(PEAR)

PHP 使用 SMTP 傳送郵件(PEAR)

WBOY
WBOY原創
2016-07-25 09:09:15866瀏覽
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