PHP handles postfix emails

Source: Internet
Author: User
01? Php0203 # read all email content from input 04 $ email =; 05 $ fd = fopen (php: // stdin, r); 06 while (! Feof ($ fd) {07 $ email. = fread ($ fd, 1024); 08} 09 fclose ($ fd); 10

 

 
  1. 01
  2.  
  3. 02
  4.  
  5. 03 # read all emails from input
  6.  
  7. 04 $ email = "";
  8.  
  9. 05 $ fd = fopen ("php: // stdin", "r ");
  10.  
  11. 06 while (! Feof ($ fd )){
  12.  
  13. 07 $ email. = fread ($ fd, 1024 );
  14.  
  15. 08}
  16.  
  17. 09 fclose ($ fd );
  18.  
  19. 10
  20.  
  21. 11 # record all content and test
  22.  
  23. 12 file_put_contents ("/tmp/mail/". time (), $ email );
  24.  
  25. 13
  26.  
  27. 14 # process emails
  28.  
  29. 15 $ lines = explode ("\ n", $ email );
  30.  
  31. 16
  32.  
  33. 17 // empty vars
  34.  
  35. 18 $ from = "";
  36.  
  37. 19 $ date = "";
  38.  
  39. 20 $ subject = "";
  40.  
  41. 21 $ message = "";
  42.  
  43. 22 $ splittingheaders = true;
  44.  
  45. 23
  46.  
  47. 24 for ($ I = 0; $ I
  48.  
  49. 25 if ($ splittingheaders ){
  50.  
  51. 26
  52.  
  53. 27 // look out for special headers
  54.  
  55. 28 if (preg_match ("/^ Subject: (. *)/", $ lines [$ I], $ matches )){
  56.  
  57. 29 $ subject = $ matches [1];
  58.  
  59. 30}
  60.  
  61. 31 if (preg_match ("/^ From: (. *)/", $ lines [$ I], $ matches )){
  62.  
  63. 32 if (strpos ($ lines [$ I], "<")){
  64.  
  65. 33 // the name exist too in from header
  66.  
  67. 34 $ data = explode ('<', $ lines [$ I]);
  68.  
  69. 35 $ from = substr (trim ($ data [1]), 0,-1 );
  70.  
  71. 36} else {
  72.  
  73. 37 // only the mail
  74.  
  75. 38 $ from = $ matches [1];
  76.  
  77. 39}
  78.  
  79. 40}
  80.  
  81. 41 if (preg_match ("/^ Date: (. *)/", $ lines [$ I], $ matches )){
  82.  
  83. 42 $ date = $ matches [1];
  84.  
  85. 43}
  86.  
  87. 44} else {
  88.  
  89. 45 // not a header, but message
  90.  
  91. 46 $ message. = $ lines [$ I]. "\ n ";
  92.  
  93. 47}
  94.  
  95. 48
  96.  
  97. 49 if (trim ($ lines [$ I]) = ""){
  98.  
  99. 50 // empty line, header section has ended
  100.  
  101. 51 $ splittingheaders = false;
  102.  
  103. 52}
  104.  
  105. 53}
  106.  
  107. 54
  108.  
  109. 55 $ when = date ("Y-m-d G: I: s ");
  110.  
  111. 56 $ data = explode ('@', $ from );
  112.  
  113. 57 $ username = $ data [0];
  114.  
  115. 58
  116.  
  117. 59 # Record to database
  118.  
  119. 60 $ SQL = "insert into mails ('username', 'from', 'subobject', 'date', 'message') values ('$ username ', '$ from',' $ subject ',' $ when', '$ message ')";
  120.  
  121. 61
  122.  
  123. 62 # Test
  124.  
  125. 63 file_put_contents ("/tmp/mail2.log", $ SQL );
  126.  
  127. 64?>

 

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.