建站学 - 轻松建站从此开始!

建站学-个人建站指南,网页制作,网站设计,网站制作教程

当前位置: 建站学 > 网站开发 > PHP教程 >

PHP如何处理postfix的邮件内容(代码)

时间:2011-05-09 09:56来源: 作者: 点击:
下面的是实现PHP处理postfix的邮件内容的代码 <?php       02         03 #从输入读取到所有的邮件内容       04 $email = "";&

下面的是实现PHP处理postfix的邮件内容的代码

<?php    

  1.  
  2. 02      
  3.  
  4. 03 #从输入读取到所有的邮件内容    
  5.  
  6. 04 $email = "";    
  7.  
  8. 05 $fd = fopen("php://stdin""r");    
  9.  
  10. 06 while (!feof($fd)) {    
  11.  
  12. 07   $email .= fread($fd, 1024);    
  13.  
  14. 08 }    
  15.  
  16. 09 fclose($fd);    
  17.  
  18. 10      
  19.  
  20. 11 #记录所有的内容,测试    
  21.  
  22. 12 file_put_contents("/tmp/mail/".time(), $email);    
  23.  
  24. 13      
  25.  
  26. 14 #处理邮件    
  27.  
  28. 15 $lines = explode("\n"$email);    
  29.  
  30. 16      
  31.  
  32. 17 // empty vars    
  33.  
  34. 18 $from = "";    
  35.  
  36. 19 $date = "";    
  37.  
  38. 20 $subject = "";    
  39.  
  40. 21 $message = "";    
  41.  
  42. 22 $splittingheaders = true;    
  43.  
  44. 23      
  45.  
  46. 24 for ($i=0; $i<count($lines); $i++) {    
  47.  
  48. 25   if ($splittingheaders) {    
  49.  
  50. 26      
  51.  
  52. 27     // look out for special headers    
  53.  
  54. 28     if (preg_match("/^Subject: (.*)/"$lines[$i], $matches)) {    
  55.  
  56. 29       $subject = $matches[1];    
  57.  
  58. 30     }    
  59.  
  60. 31     if (preg_match("/^From: (.*)/"$lines[$i], $matches)) {    
  61.  
  62. 32       if(strpos($lines[$i],"<")){    
  63.  
  64. 33         //the name exist too in from header    
  65.  
  66. 34         $data = explode('<',$lines[$i]);    
  67.  
  68. 35         $from = substr(trim($data[1]),0,-1);    
  69.  
  70. 36       }else{    
  71.  
  72. 37         //only the mail    
  73.  
  74. 38         $from = $matches[1];    
  75.  
  76. 39       }    
  77.  
  78. 40     }    
  79.  
  80. 41     if (preg_match("/^Date: (.*)/"$lines[$i], $matches)) {    
  81.  
  82. 42       $date = $matches[1];    
  83.  
  84. 43     }    
  85.  
  86. 44   } else {    
  87.  
  88. 45     // not a header, but message    
  89.  
  90. 46     $message .= $lines[$i]."\n";    
  91.  
  92. 47   }    
  93.  
  94. 48      
  95.  
  96. 49   if (trim($lines[$i])=="") {    
  97.  
  98. 50     // empty line, header section has ended    
  99.  
  100. 51     $splittingheaders = false;    
  101.  
  102. 52   }    
  103.  
  104. 53 }    
  105.  
  106. 54      
  107.  
  108. 55 $when = date("Y-m-d G:i:s");    
  109.  
  110. 56 $data = explode('@',$from);    
  111.  
  112. 57 $username = $data[0];    
  113.  
  114. 58      
  115.  
  116. 59 #记录到数据库    
  117.  
  118. 60 $sql = "insert into mails ( `username`, `from`, `subject`, `date`, `message`) values ( '$username', '$from', '$subject', '$when', '$message')";    
  119.  
  120. 61      
  121.  
  122. 62 #测试    
  123.  
  124. 63 file_put_contents("/tmp/mail2.log"$sql);    
  125.  
  126. 64 ?>   
(责任编辑:admin)
织梦二维码生成器
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片