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