PHP处理postfix邮件内容的方法

yipeiwu_com6年前PHP代码库

本文实例讲述了PHP处理postfix邮件内容的方法。分享给大家供大家参考。具体如下:

<?php
//从输入读取到所有的邮件内容
$email = "";
$fd = fopen("php://stdin", "r");
while (!feof($fd)) {
 $email .= fread($fd, 1024);
}
fclose($fd);
//记录所有的内容,测试
file_put_contents("/tmp/mail/".time(), $email);
//处理邮件
$lines = explode("\n", $email);
// empty vars
$from = "";
$date = "";
$subject = "";
$message = "";
$splittingheaders = true;
for ($i=0; $i<count($lines); $i++) {
 if ($splittingheaders) {
  // look out for special headers
  if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
   $subject = $matches[1];
  }
  if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
   if(strpos($lines[$i],"<")){
    //the name exist too in from header
    $data = explode('<',$lines[$i]);
    $from = substr(trim($data[1]),0,-1);
   }else{
    //only the mail
    $from = $matches[1];
   }
  }
  if (preg_match("/^Date: (.*)/", $lines[$i], $matches)) {
   $date = $matches[1];
  }
 } else {
  // not a header, but message
  $message .= $lines[$i]."\n";
 }
 if (trim($lines[$i])=="") {
  // empty line, header section has ended
  $splittingheaders = false;
 }
}
$when = date("Y-m-d G:i:s");
$data = explode('@',$from);
$username = $data[0];
//记录到数据库
$sql = "insert into mails ( `username`, `from`, `subject`, `date`, `message`) values ( '$username', '$from', '$subject', '$when', '$message')";
//测试
file_put_contents("/tmp/mail2.log", $sql);
?>

希望本文所述对大家的php程序设计有所帮助。

相关文章

PHP类的特性实例分析

本文实例讲述了PHP类的特性。分享给大家供大家参考,具体如下: 对象向下传递特性 当一个对象调用一个实例方法,然后在该方法中又去静态调用另一个类的方法,则在被静态调用的方法中获得源方法中...

php 分库分表hash算法

复制代码 代码如下://分库分表算法 function calc_hash_db($u, $s = 4) { $h = sprintf("%u", crc32($u)); $h1 = i...

php cookie 作用范围–不要在当前页面使用你的cookie

因为在本地测试完全没有问题,所以ssh到远程服务器上(不是发布服务器,建议不要直接在Publish Server上直接改东西),进行了一下断点的测试,最后发现是一个比较复杂的逻辑中有个函...

PHP实现表单提交时去除斜杠的方法

本文实例讲述了PHP实现表单提交时去除斜杠的方法。分享给大家供大家参考,具体如下: <html> <head> <title>HTML...

php如何执行非缓冲查询API

对于PHP的缓冲模式查询大家都知道,下面列举的例子是如何执行非缓冲查询API。 非缓冲查询方法一: mysqli <?php $mysqli = new mysqli(...