Advertisement
Guest User

Untitled

a guest
May 21st, 2017
867
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.39 KB | None | 0 0
  1. #!/usr/bin/php
  2. <?php
  3.  
  4. // Database info
  5. $hostname = 'localhost';
  6. $dbname   = 'email';
  7. $username = 'username';
  8. $password = 'password';
  9.  
  10. // Email address to send errors to
  11. $email      = "myaddress@foobar.com";
  12.  
  13. // fetch data from stdin
  14. $data = file_get_contents("php://stdin");
  15.  
  16. // extract the body
  17. // NOTE: a properly formatted email's first empty line defines the separation between the headers and the message body
  18. list($data, $body) = explode("\n\n", $data, 2);
  19.  
  20. // explode on new line
  21. $data = explode("\n", $data);
  22.  
  23. // define a variable map of known headers
  24. $patterns = array(
  25.   'Return-Path',
  26.   'X-Original-To',
  27.   'Delivered-To',
  28.   'Received',
  29.   'To',
  30.   'Message-Id',
  31.   'Date',
  32.   'From',
  33.   'Subject',
  34.   'Content-Transfer-Encoding',
  35. );
  36.  
  37. // define a variable to hold parsed headers
  38. $headers = array();
  39.  
  40. // loop through data
  41. foreach ($data as $data_line) {
  42.  
  43.   // for each line, assume a match does not exist yet
  44.   $pattern_match_exists = false;
  45.  
  46.   // check for lines that start with white space
  47.   // NOTE: if a line starts with a white space, it signifies a continuation of the previous header
  48.   if ((substr($data_line,0,1)==' ' || substr($data_line,0,1)=="\t") && $last_match) {
  49.  
  50.     // append to last header
  51.     $headers[$last_match][] = $data_line;
  52.     continue;
  53.  
  54.   }
  55.  
  56.   // loop through patterns
  57.   foreach ($patterns as $key => $pattern) {
  58.  
  59.     // create preg regex
  60.     $preg_pattern = '/^' . $pattern .': (.*)$/';
  61.  
  62.     // execute preg
  63.     preg_match($preg_pattern, $data_line, $matches);
  64.  
  65.     // check if preg matches exist
  66.     if (count($matches)) {
  67.  
  68.       $headers[$pattern][] = $matches[1];
  69.       $pattern_match_exists = true;
  70.       $last_match = $pattern;
  71.  
  72.     }
  73.  
  74.   }
  75.  
  76.   // check if a pattern did not match for this line
  77.   if (!$pattern_match_exists) {
  78.     $headers['UNMATCHED'][] = $data_line;
  79.   }
  80.  
  81. }
  82.  
  83. try {
  84.   $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
  85.  
  86.   $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  87.  
  88.   $stmt = $dbh->prepare("INSERT INTO mail(created, body, mailto, return_path, x_original_to, delivered_to, message_id, subject, maildate, mailfrom, content_transfer_encoding) VALUES (:created, :body, :mailto, :return_path, :x_original_to, :delivered_to, :message_id, :subject, :maildate, :mailfrom, :content_transfer_encoding)");
  89.  
  90.   $stmt->bindParam(':created', time(), PDO::PARAM_INT);  
  91.   $stmt->bindParam(':body', $body, PDO::PARAM_STR);  
  92.   $stmt->bindParam(':mailto', $headers['To'][0], PDO::PARAM_STR, 255);
  93.   $stmt->bindParam(':return_path', $headers['Return-Path'][0], PDO::PARAM_STR, 255);  
  94.   $stmt->bindParam(':x_original_to', $headers['X-Original-To'][0], PDO::PARAM_STR, 255);
  95.   $stmt->bindParam(':delivered_to', $headers['Delivered-To'][0], PDO::PARAM_STR, 255);
  96.   $stmt->bindParam(':message_id', $headers['Message-Id'][0], PDO::PARAM_STR, 255);
  97.   $stmt->bindParam(':subject', $headers['Subject'][0], PDO::PARAM_STR, 255);
  98.   $stmt->bindParam(':maildate', strtotime($headers['Date'][0]), PDO::PARAM_INT);
  99.   $stmt->bindParam(':mailfrom', $headers['From'][0], PDO::PARAM_STR, 255);
  100.   $stmt->bindParam(':content_transfer_encoding', $headers['Content-Transfer-Encoding'][0], PDO::PARAM_STR, 255);
  101.   $stmt->execute();
  102.   $dbh = null;
  103.  
  104.   }
  105. catch(PDOException $e)
  106.   {
  107.     // If error send email to me
  108.     mail($email, "DB ERROR: " . $headers['Subject'][0], $e->getMessage(), $header);
  109.   }
  110.  
  111. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement