Jump to content

Pipe to php script


Guest xaav

Recommended Posts

I have tried, but can't seem to get my php pipe to work. Any suggestions.

 

#!/usr/bin/php -q
<?php
    include("./main-inc.php");

try{    
    //fetchdata from stdin
    $data = file_get_contents("php://stdin");
    
    
    // extract the body
    // NOTE: a properly formatted email's first empty line defines the separation between the headers and the message body
    list($data, $body) = explode("\n\n", $data, 2);
    
    
    // explode on new line
    $data = explode("\n", $data);
    
    // define a variable map of known headers
    $patterns = array(
      'Return-Path',
      'X-Original-To',
      'Delivered-To',
      'Received',
      'To',
      'Message-Id',
      'Date',
      'From',
      'Subject',
    
    );
    
    // define a variable to hold parsed headers
    $headers = array();
    
    // loop through data
    foreach ($data as $data_line) {
    
      // for each line, assume a match does not exist yet
      $pattern_match_exists = false;
    
      // check for lines that start with white space
      // NOTE: if a line starts with a white space, it signifies a continuation of the previous header
      if ((substr($data_line,0,1)==' ' || substr($data_line,0,1)=="\t") && $last_match) {
    
        // append to last header
        $headers[$last_match][] = $data_line;
        continue;
    
      }
    
      // loop through patterns
      foreach ($patterns as $key => $pattern) {
    
        // create preg regex
        $preg_pattern = '/^' . $pattern .': (.*)$/';
    
        // execute preg
        preg_match($preg_pattern, $data_line, $matches);
    
    
        // check if preg matches exist
        if (count($matches)) { 
    
          $headers[$pattern][] = $matches[1];
          $pattern_match_exists = true;
          $last_match = $pattern;
    
    
        }
    
    
      }
    
    
      // check if a pattern did not match for this line
      if (!$pattern_match_exists) {
        $headers['UNMATCHED'][] = $data_line;
      }
    
    
    }
    
    
    $body = $body;
    $from = $headers["From"][0];
    $to = $headers["To"][0];
    
    
    
    
    function extract_emails_from($string){
     preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $string, $matches);
     return $matches[0];
    }
    
    
    $to = extract_emails_from($to);
    $from = extract_emails_from($from);
    
    $to = $to[0];
    $from = $from[0];

         .. 

}
catch(Exception $err)
{
    logToFile(PHP_ACCESS_DIR."db/errors.log", $err);
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...