This article mainly introduces the PHP reading, the regular matching message content method, the interest friend's reference, hoped to be helpful to everybody.
This article explains how PHP handles postfix message content. Specific as follows:
<?php//read from the input to all the contents of the message: ""; $fd = fopen ("Php://stdin", "R"), while (!feof ($FD)) {$email. = fread ($FD, 1024);} Fclose ($FD);//Record all contents, test file_put_contents ("/tmp/mail/". Time (), $email);//process mail $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 FR Om 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 have ended $splittIngheaders = false; }} $when = Date ("y-m-d g:i:s"), $data = explode (' @ ', $from); $username = $data [0];//record to database $sql = "INSERT into mails (' Usernam E ', ' from ', ' subject ', ' Date ', ' message ') VALUES (' $username ', ' $from ', ' $subject ', ' $when ', ' $message ') ";//Test File_put_c Ontents ("/tmp/mail2.log", $sql);? >
Summary: The above is the entire content of this article, I hope to be able to help you learn.
Related recommendations:
Basic knowledge of sockets in PHP
PHP implements the ability to send and receive text messages
The method and principle of reversible encryption in PHP