How to correctly use the PHP email sending Function

Source: Internet
Author: User
Tags php email

Example code of the PHP email sending function:

  1. # Echo send_mail ($ _ POST ["email"], $ tosubject, $ _ POST ["errortext"]);
  2. Echo send_mail ('someone @ 126.com ', 'only a test mail for test php mail function', 'spam mail ');
  3. Echo time ();
  4.  
  5. Function send_mail ($ to, $ subject = 'no subobject', $ body ){
  6. $ Loc_host = "mail. server"; // The Name Of The email sending computer.
  7. $ Smtp_acc = "smp@163.com"; // Smtp authenticated user name
  8. $ Smtp_pass = "pwdpwd"; // password for Smtp authentication, which is generally equivalent to a pop3 PASSWORD
  9. $ Smtp_host = "smtp.163.com"; // SMTP server address, similar to smtp.tom.com
  10. $ From = "smp@163.com"; // Email address of the sender, your mail address
  11. $ Deliver = $ smtp_acc; // reply to the specified email address
  12. $ Headers = "Content-Type: text/plain; charset =" gb2312 "rnContent-Transfer-Encoding: base64 ";
  13. $ Lb = "rn"; // linebreak
  14. $ Hdr = explode ($ lb, $ headers); // resolved hdr
  15. If ($ body) {$ bdy = preg_replace ("/^./", "..", explode ($ lb, $ body);} // The parsed Body
  16.  
  17. $ Smtp = array (
  18. // 1. EHLO, expecting to return 220 or 250
  19. Array ("EHLO". $ loc_host. $ lb, "220,250", "HELO error :"),
  20. // 2. send Auth Login and expect to return 334
  21. Array ("auth login". $ lb, "334", "AUTH error :"),
  22. // 3. Send the Base64-encoded user name, and a 334 error is returned.
  23. Array (base64_encode ($ smtp_acc). $ lbs, "334", "AUTHENTIFICATION error :"),
  24. // 4. Send the Base64 encoded password and expect to return 235
  25. Array (base64_encode ($ smtp_pass). $ lbs, "235", "AUTHENTIFICATION error :"));
  26. // 5. Send Mail From and expect to return 250
  27. $ Smtp [] = array ("mail from: <". $ from. ">". $ lb, "250", "mail from error :");
  28. // 6. Send Rcpt. Expected 250
  29. $ Smtp [] = array ("rcpt to: <". $ to. ">". $ lb, "250", "rcpt to error :");
  30. // 7. Send DATA and expect to return 354
  31. $ Smtp [] = array ("DATA". $ lb, "354", "DATA error :");
  32. // 8.0. Send From
  33. $ Smtp [] = array ("From:". $ deliver. $ lb ,"","");
  34. // 8.2. Send.
  35. $ Smtp [] = array ("To:". $ to. $ lb ,"","");
  36. // 8.1. Sending title
  37. $ Smtp [] = array ("Subject:". $ subject. $ lb ,"","");
  38. // 8.3. Send other Header content
  39. Foreach ($ hdr as $ h) {$ smtp [] = array ($ h. $ lb ,"","");}
  40. // 8.4. Send an empty line and end Header sending
  41. $ Smtp [] = array ($ lb ,"","");
  42. // 8.5 send email subject
  43. If ($ bdy) {foreach ($ bdy as $ B) {$ smtp [] = array (base64_encode ($ B. $ lb ). $ lb ,"","");}}
  44. // 9. Sending "." indicates that the mail is over and 250 is expected to be returned.
  45. $ Smtp [] = array (".". $ lb, "250", "DATA (end) error :");
  46. // 10. Send Quit and exit. expected to return 221
  47. $ Smtp [] = array ("QUIT". $ lb, "221", "QUIT error :");
  48.  
  49. // Open the smtp server port
  50. $ Fp = @ fsockopen ($ smtp_host, 25 );
  51. If (! $ Fp) echo "<B> Error: </B> Cannot conect to". $ smtp_host. "<br> ";
  52. While ($ result = @ fgets ($ fp, 1024) {if (substr ($ result, 3, 1) = "") {break ;}}
  53.  
  54. $ Result_str = "";
  55. // Send commands/data in the smtp Array
  56. Foreach ($ smtp as $ req ){
  57. // Send information
  58. @ Fputs ($ fp, $ req [0]);
  59. // If you want to receive the information returned by the server
  60. If ($ req [1]) {
  61. // Receive information
  62. While ($ result = @ fgets ($ fp, 1024 )){
  63. If (substr ($ result, 3, 1) = "") {break ;}
  64. };
  65. If (! Strstr ($ req [1], substr ($ result, 0, 3 ))){
  66. $ Result_str. = $ req [2]. $ result. "<br> ";
  67. }
  68. }
  69. }
  70. // Close the connection
  71. @ Fclose ($ fp );
  72. Return $ result_str;
  73. }

We hope that the above code example will allow you to fully master the application of the PHP mail sending function.
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.