How to correctly use PHP to send emails _ PHP Tutorial

Source: Internet
Author: User
Tags php email
How to correctly use the PHP email sending function. Application code example of PHP mail function: # echosend_mail ($ _ POST [email], $ tosubject, $ _ POST [errortext]); echosend_mail (someone@126.com, onlyatestmailfortestphpmailfunction, one

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

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


Pipeline # echosend_mail ($ _ POST [email], $ tosubject, $ _ POST [errortext]); echosend_mail ('Someone @ 126.com ', 'onlyatestmailfortestphpmailfunction', 'a letter...

Related Article

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.