作者:阳光-假日 | 来源:互联网 | 2013-04-22 17:18
01?php0203#从输入读取到所有的邮件内容04$email=;05$fd=fopen(php://stdin,r);06while(!feof($fd)){07$email.=fread($fd,1024);08}09fclose($fd);10
- 01
-
- 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("\n", $email);
-
- 16
-
- 17
-
- 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
-
- 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
-
- 34 $data = explode('<',$lines[$i]);
-
- 35 $from = substr(trim($data[1]),0,-1);
-
- 36 }else{
-
- 37
-
- 38 $from = $matches[1];
-
- 39 }
-
- 40 }
-
- 41 if (preg_match("/^Date: (.*)/", $lines[$i], $matches)) {
-
- 42 $date = $matches[1];
-
- 43 }
-
- 44 } else {
-
- 45
-
- 46 $message .= $lines[$i]."\n";
-
- 47 }
-
- 48
-
- 49 if (trim($lines[$i])=="") {
-
- 50
-
- 51 $splittingheaders = false;
-
- 52 }
-
- 53 }
-
- 54
-
- 55 $when = date("Y-m-d G:i:s");
-
- 56 $data = explode('@',$from);
-
- 57 $username = $data[0];
-
- 58
-
- 59 #记录到数据库
-
- 60 $sql = "insert into mails ( `username`, `from`, `subject`, `date`, `message`) values ( '$username', '$from', '$subject', '$when', '$message')";
-
- 61
-
- 62 #测试
-
- 63 file_put_contents("/tmp/mail2.log", $sql);
-
- 64 ?>