PHP method for processing postfix mail content, phppostfix mail content

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.