PHP處理postfix的郵件內容

來源:互聯網
上載者:User
 
  1. 01 <?php    
  2.  
  3. 02      
  4.  
  5. 03 #從輸入讀取到所有的郵件內容    
  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 #記錄所有的內容,測試    
  22.  
  23. 12 file_put_contents("/tmp/mail/".time(), $email);    
  24.  
  25. 13      
  26.  
  27. 14 #處理郵件    
  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<count($lines); $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 #記錄到資料庫    
  118.  
  119. 60 $sql = "insert into mails ( `username`, `from`, `subject`, `date`, `message`) values ( '$username', '$from', '$subject', '$when', '$message')";    
  120.  
  121. 61      
  122.  
  123. 62 #測試    
  124.  
  125. 63 file_put_contents("/tmp/mail2.log", $sql);    
  126.  
  127. 64 ?>   




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.