Patch to PHP mail() function
| From: | Olivier Beyssac | Date: | Tue, 28 Dec 2004 11:58:25 +0000 |
| Subject: | Patch to PHP mail() function | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-14218@lists.php.net to get a copy of this message | ||
Hi,
Please find attached a small patch to the PHP mail() function. It adds
two options to php.ini:
- mail_add_cgi_headers: add SERVER_NAME, SCRIPT_NAME and REMOTE_ADDR to
the mail headers before piping it to sendmail with the X-CGI-VAR
prefix),
- mail_custom_headers: specify additionnal, site-wide headers (i.e.:
"X-Abuse-To: abuse@site.net").
If you include this contribution to the official PHP source tree, please
mention this email address and my company name: Online.net.
Regards.
--
Olivier Beyssac - obeyssac@proxad.net
diff -urN php-4.3.10.orig/ext/standard/mail.c php-4.3.10/ext/standard/mail.c --- php-4.3.10.orig/ext/standard/mail.c Fri Jan 9 02:35:58 2004 +++ php-4.3.10/ext/standard/mail.c Thu Dec 23 16:25:32 2004 @@ -55,6 +55,16 @@ continue; \ } \ +#define env_fprintf(var, stream) \ + do { \ + char *tmp_env; \ + int tmp_len; \ + \ + if ((tmp_env = sapi_getenv(var, tmp_len TSRMLS_CC)) \ + || (tmp_env = getenv(var))) \ + fprintf(stream, "X-CGI-VAR-%s: %s\n", var, tmp_env); \ + } while (0) + /* {{{ proto int ezmlm_hash(string addr) Calculate EZMLM list hash value. */ PHP_FUNCTION(ezmlm_hash) @@ -177,6 +187,8 @@ FILE *sendmail; int ret; char *sendmail_path = INI_STR("sendmail_path"); + int add_cgi_headers = INI_BOOL("mail_add_cgi_headers"); + char *custom_headers = INI_STR("mail_custom_headers"); char *sendmail_cmd = NULL; if (!sendmail_path) { @@ -227,6 +239,17 @@ #endif fprintf(sendmail, "To: %s\n", to); fprintf(sendmail, "Subject: %s\n", subject); + + if (add_cgi_headers) { + /* Add CGI variables (if available) to the headers */ + env_fprintf("SERVER_NAME", sendmail); + env_fprintf("SCRIPT_NAME", sendmail); + env_fprintf("REMOTE_ADDR", sendmail); + } + + if (custom_headers) + fprintf(sendmail, "%s\n", custom_headers); + if (headers != NULL) { fprintf(sendmail, "%s\n", headers); } @@ -262,7 +285,9 @@ PHP_MINFO_FUNCTION(mail) { char *sendmail_path = INI_STR("sendmail_path"); - + int cgi = INI_BOOL("mail_add_cgi_headers"); + char *custom_headers = INI_STR("mail_custom_headers"); + #ifdef PHP_WIN32 if (!sendmail_path) { php_info_print_table_row(2, "Internal Sendmail Support for Windows", "enabled"); @@ -271,6 +296,8 @@ } #else php_info_print_table_row(2, "Path to sendmail", sendmail_path); + php_info_print_table_row(2, "Add CGI headers", cgi ? "Yes" : "No"); + php_info_print_table_row(2, "Custom headers", custom_headers); #endif } /* }}} */ diff -urN php-4.3.10.orig/main/main.c php-4.3.10/main/main.c --- php-4.3.10.orig/main/main.c Fri Oct 1 16:27:13 2004 +++ php-4.3.10/main/main.c Thu Dec 23 16:27:07 2004 @@ -357,6 +357,8 @@ PHP_INI_ENTRY("precision", "14", PHP_INI_ALL, OnSetPrecision) PHP_INI_ENTRY("sendmail_from", NULL, PHP_INI_ALL, NULL) PHP_INI_ENTRY("sendmail_path", DEFAULT_SENDMAIL_PATH, PHP_INI_SYSTEM, NULL) + PHP_INI_ENTRY("mail_add_cgi_headers", "0", PHP_INI_SYSTEM, NULL) + PHP_INI_ENTRY("mail_custom_headers", NULL, PHP_INI_SYSTEM, NULL) PHP_INI_ENTRY("disable_functions", "", PHP_INI_SYSTEM, NULL) PHP_INI_ENTRY("disable_classes", "", PHP_INI_SYSTEM, NULL)
diff -urN php-4.3.10.orig/ext/standard/mail.c php-4.3.10/ext/standard/mail.c --- php-4.3.10.orig/ext/standard/mail.c Fri Jan 9 02:35:58 2004 +++ php-4.3.10/ext/standard/mail.c Thu Dec 23 16:25:32 2004 @@ -55,6 +55,16 @@ continue; \ } \ +#define env_fprintf(var, stream) \ + do { \ + char *tmp_env; \ + int tmp_len; \ + \ + if ((tmp_env = sapi_getenv(var, tmp_len TSRMLS_CC)) \ + || (tmp_env = getenv(var))) \ + fprintf(stream, "X-CGI-VAR-%s: %s\n", var, tmp_env); \ + } while (0) + /* {{{ proto int ezmlm_hash(string addr) Calculate EZMLM list hash value. */ PHP_FUNCTION(ezmlm_hash) @@ -177,6 +187,8 @@ FILE *sendmail; int ret; char *sendmail_path = INI_STR("sendmail_path"); + int add_cgi_headers = INI_BOOL("mail_add_cgi_headers"); + char *custom_headers = INI_STR("mail_custom_headers"); char *sendmail_cmd = NULL; if (!sendmail_path) { @@ -227,6 +239,17 @@ #endif fprintf(sendmail, "To: %s\n", to); fprintf(sendmail, "Subject: %s\n", subject); + + if (add_cgi_headers) { + /* Add CGI variables (if available) to the headers */ + env_fprintf("SERVER_NAME", sendmail); + env_fprintf("SCRIPT_NAME", sendmail); + env_fprintf("REMOTE_ADDR", sendmail); + } + + if (custom_headers) + fprintf(sendmail, "%s\n", custom_headers); + if (headers != NULL) { fprintf(sendmail, "%s\n", headers); } @@ -262,7 +285,9 @@ PHP_MINFO_FUNCTION(mail) { char *sendmail_path = INI_STR("sendmail_path"); - + int cgi = INI_BOOL("mail_add_cgi_headers"); + char *custom_headers = INI_STR("mail_custom_headers"); + #ifdef PHP_WIN32 if (!sendmail_path) { php_info_print_table_row(2, "Internal Sendmail Support for Windows", "enabled"); @@ -271,6 +296,8 @@ } #else php_info_print_table_row(2, "Path to sendmail", sendmail_path); + php_info_print_table_row(2, "Add CGI headers", cgi ? "Yes" : "No"); + php_info_print_table_row(2, "Custom headers", custom_headers); #endif } /* }}} */ diff -urN php-4.3.10.orig/main/main.c php-4.3.10/main/main.c --- php-4.3.10.orig/main/main.c Fri Oct 1 16:27:13 2004 +++ php-4.3.10/main/main.c Thu Dec 23 16:27:07 2004 @@ -357,6 +357,8 @@ PHP_INI_ENTRY("precision", "14", PHP_INI_ALL, OnSetPrecision) PHP_INI_ENTRY("sendmail_from", NULL, PHP_INI_ALL, NULL) PHP_INI_ENTRY("sendmail_path", DEFAULT_SENDMAIL_PATH, PHP_INI_SYSTEM, NULL) + PHP_INI_ENTRY("mail_add_cgi_headers", "0", PHP_INI_SYSTEM, NULL) + PHP_INI_ENTRY("mail_custom_headers", NULL, PHP_INI_SYSTEM, NULL) PHP_INI_ENTRY("disable_functions", "", PHP_INI_SYSTEM, NULL) PHP_INI_ENTRY("disable_classes", "", PHP_INI_SYSTEM, NULL)