PHP read, regular match message content method

Source: Internet
Author: User
Tags explode
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

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.