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:
- # Echo send_mail ($ _ POST ["email"], $ tosubject, $ _ POST ["errortext"]);
- Echo send_mail ('Someone @ 126.com ', 'Only a test mail for test php mail function', 'Spam mail ');
- Echo time ();
-
- Function send_mail ($ to, $ subject = 'no subobject', $ body ){
- $ Loc_host = "mail. server"; // The name of the email sending computer.
- $ Smtp_acc = "smp@163.com"; // Smtp authenticated user name
- $ Smtp_pass = "pwdpwd"; // password for Smtp authentication, which is generally equivalent to a pop3 password
- $ Smtp_host = "smtp.163.com"; // SMTP server address, similar to smtp.tom.com
- $ From = "smp@163.com"; // Email address of the sender, your mail address
- $ Deliver = $ smtp_acc; // reply to the specified email address
- $ Headers = "Content-Type: text/plain; charset =" gb2312 "rnContent-Transfer-Encoding: base64 ";
- $ Lb = "rn"; // linebreak
- $ Hdr = explode ($ lb, $ headers); // Resolved hdr
- If ($ body) {$ bdy = preg_replace ("/^./", "..", explode ($ lb, $ body);} // The parsed Body
-
- $ Smtp = array (
- // 1. EHLO, expecting to return 220 or 250
- Array ("EHLO". $ loc_host. $ lb, "220,250", "HELO error :"),
- // 2. send Auth Login and expect to return 334
- Array ("auth login". $ lb, "334", "AUTH error :"),
- // 3. send the Base64-encoded user name, and a 334 error is returned.
- Array (base64_encode ($ smtp_acc). $ lbs, "334", "AUTHENTIFICATION error :"),
- // 4. send the Base64 encoded password and expect to return 235
- Array (base64_encode ($ smtp_pass). $ lbs, "235", "AUTHENTIFICATION error :"));
- // 5. send Mail From and expect to return 250
- $ Smtp [] = array ("mail from: <". $ from. ">". $ lb, "250", "mail from error :");
- // 6. send Rcpt. Expected 250
- $ Smtp [] = array ("rcpt to: <". $ to. ">". $ lb, "250", "rcpt to error :");
- // 7. send DATA and expect to return 354
- $ Smtp [] = array ("DATA". $ lb, "354", "DATA error :");
- // 8.0. send From
- $ Smtp [] = array ("From:". $ deliver. $ lb ,"","");
- // 8.2. send.
- $ Smtp [] = array ("To:". $ to. $ lb ,"","");
- // 8.1. sending title
- $ Smtp [] = array ("Subject:". $ subject. $ lb ,"","");
- // 8.3. send other Header content
- Foreach ($ hdr as $ h) {$ smtp [] = array ($ h. $ lb ,"","");}
- // 8.4. send an empty line and end Header sending
- $ Smtp [] = array ($ lb ,"","");
- // 8.5 send email subject
- If ($ bdy) {foreach ($ bdy as $ B) {$ smtp [] = array (base64_encode ($ B. $ lb ). $ lb ,"","");}}
- // 9. sending "." indicates that the Mail is over and 250 is expected to be returned.
- $ Smtp [] = array (".". $ lb, "250", "DATA (end) error :");
- // 10. send Quit and exit. expected to return 221
- $ Smtp [] = array ("QUIT". $ lb, "221", "QUIT error :");
-
- // Open the smtp server port
- $ Fp = @ fsockopen ($ smtp_host, 25 );
- If (! $ Fp) echo"Error:Cannot conect to ". $ smtp_host ."
";
- While ($ result = @ fgets ($ fp, 1024) {if (substr ($ result, 3, 1) = "") {break ;}}
-
- $ Result_str = "";
- // Send commands/data in the smtp array
- Foreach ($ smtp as $ req ){
- // Send information
- @ Fputs ($ fp, $ req [0]);
- // If you want to receive the information returned by the server
- If ($ req [1]) {
- // Receive information
- While ($ result = @ fgets ($ fp, 1024 )){
- If (substr ($ result, 3, 1) = "") {break ;}
- };
- If (! Strstr ($ req [1], substr ($ result, 0, 3 ))){
- $ Result_str. = $ req [2]. $ result ."
";
- }
- }
- }
- // Close the connection
- @ Fclose ($ fp );
- Return $ result_str;
- }
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...