Re: Default Return-Path with mail() and qmail
| From: | Andi Gutmans | Date: | Sat, 07 Dec 2002 15:56:50 +0000 |
| Subject: | Re: Default Return-Path with mail() and qmail | ||
| References: | 1 2 3 4 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-92187@lists.php.net to get a copy of this message | ||
Are you sure you should be using malloc()/free() and not emalloc()/efree()?
Also please use strlcpy() instead of strncpy(). (Weird I mentioned it twice in one day :)
http://www.courtesan.com/todd/papers/strlcpy.html
Andi
At 04:54 PM 12/7/2002 +0100, Daniel Lorch wrote:
--- php-4.2.3/ext/standard/mail.c Sat Aug 24 13:38:13 2002 +++ php-4.2.3-daniel/ext/standard/mail.c Mon Dec 2 01:24:35 2002@@ -21,6 +21,7 @@ #include <stdlib.h> #include <ctype.h> #include <stdio.h> +#include <string.h> #include "php.h" #include "ext/standard/info.h" #if !defined(PHP_WIN32) @@ -124,6 +125,84 @@ } /* }}} */ +char *get_header_value(char *line) +{+ while(*line && *line != ':') + line++;++ if(!*line) + return NULL;++ line++;++ while(isspace(*line)) + line++;++ return strdup(line);+} + +char *get_header(char *header_name, char *headers) +{+ char *line, *value=NULL; + int header_name_len = strlen(header_name); + int len=0;++ do { + if(*headers == '\n' || *headers == '\0') { + if(len < header_name_len) { + len = 0; + continue; + }++ if((line = (char *)malloc(len + 1)) == NULL) + return NULL;++ headers -= len;++ strncpy(line, headers, len); + line[len] = '\0';++ headers += len;++ if(strncmp(line, header_name, header_name_len) == 0) { + value = get_header_value(line); + }++ free(line);++ len = 0; + } else { + ++len; + } + } while(*headers++ && value == NULL);++ return value;+} + +char *extract_address(char *address) +{+ char *start, *stop, *tmp;++ if((start = stop = strchr(address, '@')) == NULL) + return NULL;++ while(start >= address && !isspace(*start) && *start != '<') + start--;++ start++;++ while(*stop && !isspace(*stop) && *stop != '>') + stop++;++ if((tmp = (char *)malloc(stop - start + 1)) == NULL) + return NULL;++ strncpy(tmp, start, stop - start); + tmp[stop-start] = '\0';++ return tmp;+} + /* {{{ php_mail */ PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char *extra_cmd) @@ -135,6 +214,8 @@int ret; char *sendmail_path = INI_STR("sendmail_path"); char *sendmail_cmd = NULL; + char *return_path; + char *address;if (!sendmail_path) {#ifdef PHP_WIN32 @@ -169,6 +250,25 @@fprintf(sendmail, "To: %s\n", to); fprintf(sendmail, "Subject: %s\n", subject); if (headers != NULL) {++ /* Existing Return-Path should not be overwritten */ + if((return_path = get_header("Return-Path", headers)) != NULL) { + free(return_path); + } + else {++ if((return_path = get_header("From", headers)) != NULL) {++ if((address = extract_address(return_path)) != NULL) { + fprintf(sendmail, "Return-Path: <%s>\n", address); + free(address); + }++ free(return_path);++ } + }+fprintf(sendmail, "%s\n", headers); } fprintf(sendmail, "\n%s\n", message);-- PHP Development Mailing List <http://www.php.net/> To unsubscribe, visit: http://www.php.net/unsub.php