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上传图片生成缩略图(GD库)

首先来一段简单的php上传图片生成缩略图的详细代码,分享给大家供大家参考,具体内容如下 <?php function createThumbnail($imageDir...

PHP读写文件高并发处理操作实例详解

本文实例讲述了PHP读写文件高并发处理操作。分享给大家供大家参考,具体如下: 背景: 最近公司游戏开发需要知道游戏加载的流失率。因为,我们做的是网页游戏。玩过网页游戏的人都知道,进入游戏...

使用bcompiler对PHP文件进行加密的代码

使用说明: //载入函式 include_once('phpCodeZip.php'); //建立加密文件(sourceDir要加密的php文件目录,targetDir加密后的文件目录)...

PHP 杂谈《重构-改善既有代码的设计》之四 简化条件表达式

PHP 杂谈《重构-改善既有代码的设计》之四 简化条件表达式

思维导图 点击下图,查看大图。  介绍    条件逻辑有可能十分复杂,因此本章提供一些重构的手法,专门用来简化它们。   全文简述(你可直接跳...

PHP+redis实现添加处理投票的方法

本文实例讲述了PHP+redis实现添加处理投票的方法。分享给大家供大家参考,具体如下: <?php header("Content-Type:text/html;c...