diff -ruN php-5.2.14/ext/standard/mail.c php-5.2.14-new/ext/standard/mail.c
--- php-5.2.14/ext/standard/mail.c	2010-07-19 09:38:53.000000000 -0400
+++ php-5.2.14-new/ext/standard/mail.c	2010-08-12 15:21:29.000000000 -0400
@@ -23,6 +23,9 @@
 #include <stdio.h>
 #include "php.h"
 #include "ext/standard/info.h"
+#include "ext/standard/php_string.h"
+#include "ext/standard/basic_functions.h"
+
 
 #if HAVE_SYSEXITS_H
 #include <sysexits.h>
@@ -68,6 +71,8 @@
 		*p = ' ';				\
 	}						\
 
+extern long php_getuid(void);
+
 
 /* {{{ proto int ezmlm_hash(string addr)
    Calculate EZMLM list hash value. */
@@ -98,8 +103,8 @@
 {
 	char *to=NULL, *message=NULL, *headers=NULL, *headers_trimmed=NULL;
 	char *subject=NULL, *extra_cmd=NULL;
-	int to_len, message_len, headers_len;
-	int subject_len, extra_cmd_len, i;
+    int to_len, message_len, headers_len = 0;
+    int subject_len, extra_cmd_len = 0, i;
 	char *force_extra_parameters = INI_STR("mail.force_extra_parameters");
 	char *to_r, *subject_r;
 	char *p, *e;
@@ -212,25 +217,69 @@
 	int ret;
 	char *sendmail_path = INI_STR("sendmail_path");
 	char *sendmail_cmd = NULL;
+    char *mail_log = INI_STR("mail.log");
+    char *hdr = headers;
+
 #if PHP_SIGCHILD
 	void (*sig_handler)() = NULL;
 #endif
 
+#define MAIL_RET(val) \
+       if (hdr != headers) {   \
+               efree(hdr);     \
+       }       \
+       return val;     \
+
+       if (mail_log) {
+               char *tmp;
+               int l = spprintf(&tmp, 0, "mail() on [%s:%d]: To: %s -- Headers: %s\n", zend_get_executed_filename(TSRMLS_C), zend_get_executed_lineno(TSRMLS_C), to, hdr ? hdr : "");
+               php_stream *stream = php_stream_open_wrapper(mail_log, "a", IGNORE_URL_WIN | REPORT_ERRORS | STREAM_DISABLE_OPEN_BASEDIR, NULL);
+
+               if (hdr) { /* find all \r\n instances and replace them with spaces, so a log line is always one line long */ 
+                       char *p = tmp;
+                       while ((p = strpbrk(p, "\r\n"))) {
+                               *p = ' ';
+                       }
+                       tmp[l - 1] = '\n';
+               }
+               if (stream) {
+                       php_stream_write(stream, tmp, l);
+                       php_stream_close(stream);
+               }
+               efree(tmp);
+       }
+       if (PG(mail_x_header)) {
+               char *tmp = zend_get_executed_filename(TSRMLS_C);
+               char *f;
+               size_t f_len;
+
+               php_basename(tmp, strlen(tmp), NULL, 0,&f, &f_len TSRMLS_CC);
+
+               if (headers != NULL) {
+                       spprintf(&hdr, 0, "X-PHP-Originating-Script: %ld:%s\n%s", php_getuid(), f, headers);
+               } else {
+                       spprintf(&hdr, 0, "X-PHP-Originating-Script: %ld:%s\n", php_getuid(), f);
+
+               }
+               efree(f);
+       }
+
+
 	if (!sendmail_path) {
 #if (defined PHP_WIN32 || defined NETWARE)
 		/* handle old style win smtp sending */
-		if (TSendMail(INI_STR("SMTP"), &tsm_err, &tsm_errmsg, headers, subject, to, message, NULL, NULL, NULL TSRMLS_CC) == FAILURE) {
+        if (TSendMail(INI_STR("SMTP"), &tsm_err, &tsm_errmsg, hdr, subject, to, message, NULL, NULL, NULL TSRMLS_CC) == FAILURE) {
 			if (tsm_errmsg) {
 				php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", tsm_errmsg);
 				efree(tsm_errmsg);
 			} else {
 				php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", GetSMErrorText(tsm_err));
 			}
-			return 0;
+			MAIL_RET(0);
 		}
-		return 1;
+		MAIL_RET(1);
 #else
-		return 0;
+		MAIL_RET(0);
 #endif
 	}
 	if (extra_cmd != NULL) {
@@ -278,8 +327,8 @@
 #endif
 		fprintf(sendmail, "To: %s\n", to);
 		fprintf(sendmail, "Subject: %s\n", subject);
-		if (headers != NULL) {
-			fprintf(sendmail, "%s\n", headers);
+		if (hdr != NULL) {
+			fprintf(sendmail, "%s\n", hdr);
 		}
 		fprintf(sendmail, "\n%s\n", message);
 		ret = pclose(sendmail);
@@ -301,9 +350,9 @@
 #endif
 #endif
 		{
-			return 0;
+			MAIL_RET(0);
 		} else {
-			return 1;
+			MAIL_RET(1);
 		}
 	} else {
 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not execute mail delivery program '%s'", sendmail_path);
@@ -312,10 +361,10 @@
 			signal(SIGCHLD, sig_handler);						
 		}
 #endif
-		return 0;
+		MAIL_RET(0);
 	}
 
-	return 1; /* never reached */
+	MAIL_RET(1); /* never reached */
 }
 /* }}} */
 
diff -ruN php-5.2.14/main/main.c php-5.2.14-new/main/main.c
--- php-5.2.14/main/main.c	2010-06-19 16:47:24.000000000 -0400
+++ php-5.2.14-new/main/main.c	2010-08-12 15:17:48.000000000 -0400
@@ -445,6 +445,10 @@
 
 	PHP_INI_ENTRY("SMTP",						"localhost",PHP_INI_ALL,		NULL)
 	PHP_INI_ENTRY("smtp_port",					"25",		PHP_INI_ALL,		NULL)
+    STD_PHP_INI_BOOLEAN("mail.add_x_header",                        "0",            PHP_INI_SYSTEM|PHP_INI_PERDIR,          OnUpdateBool,                   mail_x_header,                  php_core_globals,       core_globals)
+    STD_PHP_INI_ENTRY("mail.log",                                   NULL,           PHP_INI_SYSTEM|PHP_INI_PERDIR,          OnUpdateString,                 mail_log,                       php_core_globals,       core_globals)
+
+
 	PHP_INI_ENTRY("browscap",					NULL,		PHP_INI_SYSTEM,		NULL)
 	PHP_INI_ENTRY("memory_limit",				"128M",		PHP_INI_ALL,		OnChangeMemoryLimit)
 	PHP_INI_ENTRY("precision",					"14",		PHP_INI_ALL,		OnSetPrecision)
diff -ruN php-5.2.14/main/php_globals.h php-5.2.14-new/main/php_globals.h
--- php-5.2.14/main/php_globals.h	2010-01-03 04:23:27.000000000 -0500
+++ php-5.2.14-new/main/php_globals.h	2010-08-12 15:17:48.000000000 -0400
@@ -161,6 +161,9 @@
 #endif
 	long max_input_nesting_level;
 	zend_bool in_user_include;
+        zend_bool mail_x_header;
+        char *mail_log;
+
 	zend_bool in_error_log;
 };
 
diff -ruN php-5.2.14/php.ini-dist php-5.2.14-new/php.ini-dist
--- php-5.2.14/php.ini-dist	2009-11-05 08:29:34.000000000 -0500
+++ php-5.2.14-new/php.ini-dist	2010-08-12 15:17:48.000000000 -0400
@@ -713,6 +713,12 @@
 ; to the sendmail binary. These parameters will always replace the value of
 ; the 5th parameter to mail(), even in safe mode.
 ;mail.force_extra_parameters =
+; Add X-PHP-Originaiting-Script: that will include uid of the script followed by the filename
+mail.add_x_header = Off
+; Log all mail() calls including the full path of the script, line #, to address and headers
+mail.log =
+
+
 
 [SQL]
 sql.safe_mode = Off
