How to correctly use PHP to send mail function _php Tutorial

Source: Internet
Author: User
Tags php send mail server port

Examples of application code for PHP send mail functions:

  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 ');
  3. echo Time ();
  4. function Send_mail ($to, $subject = ' No subject ', $body) {
  5. $ Loc_host = "Mail.server" ;//Computer name, can be arbitrary
  6. $ SMTP_ACC = "Smp@163.com" ;//smtp Authenticated user name
  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" / //email address of sender, your e-mail address
  10. $ Deliver = $SMTP _acc;//reply to the specified mailbox
  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 body after parsing
  15. $ SMTP = Array (
  16. 1, EHLO, looking forward to return 220 or 250
  17. Array ("EHLO". $loc _host. $lb, "220,250", "HELO Error:"),
  18. 2, send Auth Login, look forward to return 334
  19. Array ("AUTH LOGIN". $lb, "334", "AUTH Error:"),
  20. 3, send the BASE64 encoded user name, expect to return 334
  21. Array (Base64_encode ($smtp _acc). $lb, "334", "Authentification Error:"),
  22. 4, send the password encoded by BASE64, expect to return 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," "," "MAIL from Error:");
  26. 6, send rcpt to. Expect to return 250
  27. $SMTP [] = Array ("RCPT to: <"$to. " > ". $lb," "," RCPT to Error: ");
  28. 7, send data, look forward 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 the 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, end header send
  39. $SMTP [] = Array ($lb, "", "");
  40. 8.5. Sending the subject of the letter
  41. if ($bdy) {foreach ($bdy as $b) {$SMTP [] = Array (Base64_encode ($b. $lb). $lb, "", "");}}
  42. 9, send "." Indicates the end of the letter, expecting to return 250
  43. $SMTP [] = Array (".". $LB, "+", "DATA (end) Error:");
  44. 10, send quit, quit, look forward to return 221
  45. $SMTP [] = Array ("Quit". $lb, "221", "Quit Error:");
  46. Open the SMTP server port
  47. $ FP = @fsockopen ($smtp _host);
  48. if (! $fp) echo " < b > Error: b> cannot conect to ". $smtp _host." < BR > ";
  49. While ($result = @fgets ($fp, 1024x768)) {if (substr ($result, 3,1) = = "") {break;}}
  50. $ Result_str = "" ;
  51. Sending commands/Data in an SMTP array
  52. foreach ($smtp as $req) {
  53. Send Message
  54. @fputs ($fp, $req [0]);
  55. If you need to receive server return information,
  56. if ($req [1]) {
  57. Receive information
  58. While ($result = @fgets ($fp, 1024x768)) {
  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 connection
  67. @fclose ($FP);
  68. return $result _str;
  69. }

I hope this code example above will allow you to fully grasp the use of php send mail functions.


http://www.bkjia.com/PHPjc/446174.html www.bkjia.com true http://www.bkjia.com/PHPjc/446174.html techarticle example of application code for PHP send mail function: #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.