#!/usr/bin/php
<?php
/* Sendmail Wrapper 1.0
By Greg Maclellan (www.gregmaclellan.com)

This is a wrapper for sendmail. All you have to do is set the paths below,
and set up this script to be called.

For php, it means changing sendmail_path to point at this file. 
*/

// unique message id to use for this message
$messageid uniqid("logged");

// log file to write to (should use $messageid)
$logfile "/var/log/phpmail/".$messageid;

// path to sendmail 
$sendmail "/usr/sbin/sendmail -t -i";

// additional headers
$add_headers["X-MsgID"] = $messageid;

// parse out the domain from the current working directory. In this case, named /home/httpd/vhosts/domain.com/. 
if (preg_match("|/home/httpd/vhosts/([a-zA-Z0-9\._-]*)(/.*)?|"$_ENV["PWD"], $matches)) {
    
$add_headers["X-Generating-Domain"] = $matches[1];
}

/*****************************************************************************
 *****************************************************************************/

// read STDIN (mail message)
while (!feof(STDIN)) {
    
$data .= fread(STDIN1024);
}

// split out headers
list ($headers$message) = explode("\n\n"$data2); 

// add additional headers
if ($add_headers) {
    foreach (
$add_headers as $field=>$contents) {
        
$headers .= "\n".$field.": ".$contents;
    }
}

// reassemble the message
$data $headers."\n\n".$message;


// write to our log file
$fd fopen($logfile,"w");
fwrite($fd"Date: ".date("r")."\n");
fwrite($fd"PWD: ".$_ENV['PWD']."\n");
fwrite($fd"Msg ID: ".$messageid."\n");
fwrite($fd"\n");

fwrite($fd"Message:\n");
fwrite($fd"----------\n");
fwrite($fd$headers);
fwrite($fd"----------\n");
fwrite($fd"Body: ".strlen($message)." bytes");

fclose($fd);

// write to the real sendmail
$h popen($sendmail"w");
fwrite($h$data);
pclose($h);


?>