- 01 <?php
- 02
- 03 #從輸入讀取到所有的郵件內容
- 04 $email = "";
- 05 $fd = fopen("php://stdin", "r");
- 06 while (!feof($fd)) {
- 07 $email .= fread($fd, 1024);
- 08 }
- 09 fclose($fd);
- 10
- 11 #記錄所有的內容,測試
- 12 file_put_contents("/tmp/mail/".time(), $email);
- 13
- 14 #處理郵件
- 15 $lines = explode(" ", $email);
- 16
- 17 // empty vars
- 18 $from = "";
- 19 $date = "";
- 20 $subject = "";
- 21 $message = "";
- 22 $splittingheaders = true;
- 23
- 24 for ($i=0; $i<count($lines); $i++) {
- 25 if ($splittingheaders) {
- 26
- 27 // look out for special headers
- 28 if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
- 29 $subject = $matches[1];
- 30 }
- 31 if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
- 32 if(strpos($lines[$i],"<")){
- 33 //the name exist too in from header
- 34 $data = explode(<,$lines[$i]);
- 35 $from = substr(trim($data[1]),0,-1);
- 36 }else{
- 37 //only the mail
- 38 $from = $matches[1];
- 39 }