PHP method for processing postfix mail content, phppostfix mail content
This example describes how PHP processes the postfix content. Share it with you for your reference. The details are as follows:
<? Php // read all email content from input $ email = ""; $ fd = fopen ("php: // stdin", "r"); while (! Feof ($ fd) {$ email. = fread ($ fd, 1024) ;}fclose ($ fd); // record all content, test file_put_contents ("/tmp/mail /". time (), $ email); // process the 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]; // record to database $ SQL = "insert into mails ('username', 'from', 'subobject', 'date ', 'message') values ('$ username',' $ from', '$ subject', '$ when',' $ message ')"; // test file_put_contents ("/tmp/mail2.log", $ SQL);?>
I hope this article will help you with php programming.