Home  >  Article  >  Backend Development  >  How to correctly use PHP to send emails_PHP Tutorial

How to correctly use PHP to send emails_PHP Tutorial

WBOY
WBOYOriginal
2016-07-15 13:31:51778browse

Application code example of PHP email 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','a spam mail');
  3. echo time();
  4. function send_mail($to, $subject = 'No subject', $body) {
  5. $loc_host = "mail .server"; //Sending computer name, feel free to
  6. $smtp_acc = "smp@163.com"; //Smtp authenticated username
  7. $smtp_pass= "pwdpwd"; //Smtp authentication password, generally equivalent to pop3 password
  8. $smtp_host ="smtp.163.com"; //SMTP server address, similar to smtp.tom.com
  9. $from="smp@163.com"; //Sender’s email address, your sending mailbox 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); //Parsed hdr
  14. if($body) {$bdy = preg_replace("/^./",". .",explode($lb,$body));}//Parsed Body
  15. $smtp = array(
  16. //1. EHLO, expect to return 220 or 250
  17. array("EHLO ".$loc_host.$lb,"220,250","HELO error: "),
  18. //2. Send Auth Login , expecting to return 334
  19. array("AUTH LOGIN".$lb,"334","AUTH error:"),
  20. / /3. Send the Base64-encoded username, expecting to return 334
  21. array(base64_encode($smtp_acc).$lb,"334","AUTHENTIFICATION error : "),
  22. //4. Send the Base64-encoded password and expect a return of 235
  23. array(base64_encode($smtp_pass).$lb,"235" ,"AUTHENTIFICATION error : "));
  24. //5. Send Mail From, expecting to return 250
  25. $smtp[] = array ("MAIL FROM: <".$from.">".$lb,"250","MAIL FROM error: ");
  26. //6. Send Rcpt To.Expect to return 250
  27. $smtp[] = array("RCPT TO: <".$to.">".$lb,"250","RCPT TO error: ");
  28. //7. Send DATA, expecting 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 To
  33. $smtp[] = array("To: ".$to.$lb,"","");
  34. //8.1. Send 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 a blank line to end the header sending
  39. $ smtp[] = array($lb,"","");
  40. //8.5. Sending message body
  41. if($ bdy) {foreach($bdy as $b) {$smtp[] = array(base64_encode($b.$lb).$lb,"","");}}
  42. //9. Send "." to indicate the end of the letter and expect to return 250
  43. $smtp[] = array(".".$lb,"250","DATA( end)error: ");
  44. //10. Send Quit, exit, and expect to return 221
  45. $smtp[] = array( "QUIT".$lb,"221","QUIT error: ");
  46. //Open smtp server port
  47. $fp = @fsockopen($smtp_host, 25);
  48. if (!$ fp) echo "<b>Error: b> Cannot conect to ".$smtp_host."<br> ";
  49. while($result = @fgets($fp, 1024)){if(substr($result ,3,1) == " ") { break; }}
  50. $result_str ="";
  51. //Send the command/data in the smtp array
  52. foreach($smtp as $req){
  53. //Send message
  54. @fputs($fp, $req[0 ]);
  55. //If you need to receive information returned by the server, then
  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."<br>" ;
  63. }
  64. }
  65. }
  66. //Close the connection
  67. @fclose($fp);
  68. return $result_str;
  69. }

I hope the above code example can help everyone fully grasp the use of PHP's email sending function.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446174.htmlTechArticleApplication code example of PHP email function: #echosend_mail($_POST[email],$tosubject,$_POST[ errortext]); echosend_mail('someone@126.com','onlyatestmailfortestphpmailfunction','A letter...
Statement:
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