php讀取、正規符合郵件內容的方法

墨辰丷
發布: 2023-03-31 12:00:02
原創
2816 人瀏覽過

本篇主要介紹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(&#39;<&#39;,$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(&#39;@&#39;,$from);
$username = $data[0];
//记录到数据库
$sql = "insert into mails ( `username`, `from`, `subject`, `date`, `message`) values ( &#39;$username&#39;, &#39;$from&#39;, &#39;$subject&#39;, &#39;$when&#39;, &#39;$message&#39;)";
//测试
file_put_contents("/tmp/mail2.log", $sql);
?>
登入後複製

#總結:以上就是這篇文章的全部內容,希望能對大家的學習有所幫助。

相關推薦:

PHP中Socket的基礎知識

##php實現發送與接收簡訊的功能

php可逆加密的方法及原則

以上是php讀取、正規符合郵件內容的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!