当前位置:首页 > PHP教程 > php应用 > 列表

PHP处理postfix邮件内容的方法

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-28 11:53:28 浏览: 评论:0 

这篇文章主要介绍了PHP处理postfix邮件内容的方法,涉及php读取、正则匹配邮件内容的相关技巧,需要的朋友可以参考下,本文实例讲述了PHP处理postfix邮件内容的方法,分享给大家供大家参考,具体如下:

  1. <?php 
  2. //从输入读取到所有的邮件内容 
  3. $email = ""; 
  4. $fd = fopen("php://stdin", "r"); 
  5. while (!feof($fd)) { 
  6.  $email .= fread($fd, 1024); 
  7. } 
  8. fclose($fd); 
  9. //记录所有的内容,测试 
  10. file_put_contents("/tmp/mail/".time(), $email); 
  11. //处理邮件 
  12. $lines = explode("\n", $email); 
  13. // empty vars 
  14. $from = ""; 
  15. $date = ""; 
  16. $subject = ""; 
  17. $message = ""; 
  18. $splittingheaders = true; 
  19. for ($i=0; $i<count($lines); $i++) { 
  20.  if ($splittingheaders) { 
  21.   // look out for special headers 
  22.   if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) { 
  23.    $subject = $matches[1]; 
  24.   } 
  25.   if (preg_match("/^From: (.*)/", $lines[$i], $matches)) { 
  26.    if(strpos($lines[$i],"<")){ 
  27.     //the name exist too in from header 
  28.     $data = explode('<',$lines[$i]); 
  29.     $from = substr(trim($data[1]),0,-1); 
  30.    }else{ 
  31.     //only the mail 
  32.     $from = $matches[1]; 
  33.    } 
  34.   } 
  35.   if (preg_match("/^Date: (.*)/", $lines[$i], $matches)) { 
  36.    $date = $matches[1]; 
  37.   } 
  38.  } else { 
  39.   // not a header, but message 
  40.   $message .= $lines[$i]."\n"; 
  41.  } 
  42.  if (trim($lines[$i])=="") { 
  43.   // empty line, header section has ended 
  44.   $splittingheaders = false; 
  45.  } 
  46. } 
  47. $when = date("Y-m-d G:i:s"); 
  48. $data = explode('@',$from); 
  49. $username = $data[0]; 
  50. //记录到数据库 
  51. $sql = "insert into mails ( `username`, `from`, `subject`, `date`, `message`) values ( '$username', '$from', '$subject', '$when', '$message')"; 
  52. //测试 
  53. file_put_contents("/tmp/mail2.log", $sql); 
  54. ?>

Tags: PHP邮件内容 postfix

分享到: