html5中文学习网

您的位置: 首页 > 网络编程 > PHP编程 » 正文

PHP处理postfix的邮件内容_PHP教程_编程技术

[ ] 已经帮助:人解决问题
  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 ?>   


EhhHTML5中文学习网 - HTML5先行者学习网
EhhHTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助