Home > Backend Development > PHP Tutorial > PHP uses SMTP to send emails (PEAR)

PHP uses SMTP to send emails (PEAR)

WBOY
Release: 2016-07-25 09:09:15
Original
869 people have browsed it
Most PHPer uses the mail function to send emails, but we can use other SMTP servers to send emails. It is recommended here PEAR’s mail package to send emails.
  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());}
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