PHP處理postfix郵件內(nèi)容的方法
更新時間:2015年06月16日 17:38:23 作者:紅薯
這篇文章主要介紹了PHP處理postfix郵件內(nèi)容的方法,涉及php讀取、正則匹配郵件內(nèi)容的相關(guān)技巧,需要的朋友可以參考下
本文實例講述了PHP處理postfix郵件內(nèi)容的方法。分享給大家供大家參考。具體如下:
<?php
//從輸入讀取到所有的郵件內(nèi)容
$email = "";
$fd = fopen("php://stdin", "r");
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);
//記錄所有的內(nèi)容,測試
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];
//記錄到數(shù)據(jù)庫
$sql = "insert into mails ( `username`, `from`, `subject`, `date`, `message`) values ( '$username', '$from', '$subject', '$when', '$message')";
//測試
file_put_contents("/tmp/mail2.log", $sql);
?>
希望本文所述對大家的php程序設(shè)計有所幫助。
相關(guān)文章
php實現(xiàn)不通過擴(kuò)展名準(zhǔn)確判斷文件類型的方法【finfo_file方法與二進(jìn)制流】
這篇文章主要介紹了php實現(xiàn)不通過擴(kuò)展名準(zhǔn)確判斷文件類型的方法,涉及php中finfo_file方法與二進(jìn)制流針對文件類型的相關(guān)操作技巧,需要的朋友可以參考下2017-04-04
PHP開發(fā)中AJAX技術(shù)的簡單應(yīng)用
這篇文章主要介紹了PHP開發(fā)中AJAX技術(shù)的簡單應(yīng)用,簡單對ajax的執(zhí)行原理、實際應(yīng)用作介紹,感興趣的小伙伴們可以參考一下2015-12-12
php文件包含目錄配置open_basedir的使用與性能詳解
下面小編就為大家?guī)硪黄猵hp文件包含目錄配置open_basedir的使用與性能詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
php中isset與empty函數(shù)的困惑與用法分析
這篇文章主要介紹了php中isset與empty函數(shù)的困惑與用法,結(jié)合實例形式分析了php中isset與empty函數(shù)的功能、用法、區(qū)別及相關(guān)使用注意事項,需要的朋友可以參考下2019-07-07

