//******************************************************************** // Name: TSendMail // Input: 1) host: Name of the mail host where the SMTP server resides // max accepted length of name = 256 // 2) appname: Name of the application to use in the X-mailer // field of the message. if NULL is given the application // name is used as given by the GetCommandLine() function // max accespted length of name = 100 // Output: 1) error: Returns the error code if something went wrong or // SUCCESS otherwise. // // See SendText() for additional args! //******************************************************************** int TSendMail(char *host, int *error, char *headers, char *Subject, char *mailTo, char *data) { int ret; char *RPath = NULL; TLS_VARS; GLOBAL(WinsockStarted) = FALSE; if (host == NULL) { *error = BAD_MAIL_HOST; return BAD_MAIL_HOST; } else if (strlen(host) >= HOST_NAME_LEN) { *error = BAD_MAIL_HOST; return BAD_MAIL_HOST; } else { strcpy(GLOBAL(MailHost), host); } if (php3_ini.sendmail_from){ RPath = estrdup(php3_ini.sendmail_from); } else { return 19; } // attempt to connect with mail host *error = MailConnect(); if (*error != 0) { if(RPath)efree(RPath); return *error; } else { ret = SendText(RPath, Subject, mailTo, data, headers); TSMClose(); if (ret != SUCCESS) { *error = ret; } if(RPath)efree(RPath); return ret; } }
/********************************************************************* // Name: SendText // Input: 1) RPath: return path of the message // Is used to fill the "Return-Path" and the // "X-Sender" fields of the message. // 2) Subject: Subject field of the message. If NULL is given // the subject is set to "No Subject" // 3) mailTo: Destination address // 4) data: Null terminated string containing the data to be send. // 5,6) headers of the message. Note that the second // parameter, headers_lc, is actually a lowercased version of // headers. The should match exactly (in terms of length), // only differ in case // Output: Error code or SUCCESS // Description: // Author/Date: jcar 20/9/96 // History: //*******************************************************************/ static int SendText(char *RPath, char *Subject, char *mailTo, char *mailCc, char *mailBcc, char *data, char *headers, char *headers_lc, char **error_message) { int res; char *p; char *tempMailTo, *token, *pos1, *pos2; char *server_response = NULL; char *stripped_header = NULL; zend_string *data_cln; /* check for NULL parameters */ if (data == NULL) return (BAD_MSG_CONTENTS); if (mailTo == NULL) return (BAD_MSG_DESTINATION); if (RPath == NULL) return (BAD_MSG_RPATH); /* simple checks for the mailto address */ /* have ampersand ? */ /* mfischer, 20020514: I commented this out because it really seems bogus. Only a username for example may still be a valid address at the destination system. if (strchr(mailTo, '@') == NULL) return (BAD_MSG_DESTINATION); */ snprintf(PW32G(mail_buffer), sizeof(PW32G(mail_buffer)), "HELO %s\r\n", PW32G(mail_local_host)); /* in the beginning of the dialog */ /* attempt reconnect if the first Post fail */ if ((res = Post(PW32G(mail_buffer))) != SUCCESS) { int err = MailConnect(); if (0 != err) { return (FAILED_TO_SEND); } if ((res = Post(PW32G(mail_buffer))) != SUCCESS) { return (res); } } if ((res = Ack(&server_response)) != SUCCESS) { SMTP_ERROR_RESPONSE(server_response); return (res); } SMTP_SKIP_SPACE(RPath); FormatEmailAddress(PW32G(mail_buffer), RPath, "MAIL FROM:<%s>\r\n"); if ((res = Post(PW32G(mail_buffer))) != SUCCESS) { return (res); } if ((res = Ack(&server_response)) != SUCCESS) { SMTP_ERROR_RESPONSE(server_response); return W32_SM_SENDMAIL_FROM_MALFORMED; } tempMailTo = estrdup(mailTo); /* Send mail to all rcpt's */ token = strtok(tempMailTo, ","); while (token != NULL) { SMTP_SKIP_SPACE(token); FormatEmailAddress(PW32G(mail_buffer), token, "RCPT TO:<%s>\r\n"); if ((res = Post(PW32G(mail_buffer))) != SUCCESS) { efree(tempMailTo); return (res); } if ((res = Ack(&server_response)) != SUCCESS) { SMTP_ERROR_RESPONSE(server_response); efree(tempMailTo); return (res); } token = strtok(NULL, ","); } efree(tempMailTo); if (mailCc && *mailCc) { tempMailTo = estrdup(mailCc); /* Send mail to all rcpt's */ token = strtok(tempMailTo, ","); while (token != NULL) { SMTP_SKIP_SPACE(token); FormatEmailAddress(PW32G(mail_buffer), token, "RCPT TO:<%s>\r\n"); if ((res = Post(PW32G(mail_buffer))) != SUCCESS) { efree(tempMailTo); return (res); } if ((res = Ack(&server_response)) != SUCCESS) { SMTP_ERROR_RESPONSE(server_response); efree(tempMailTo); return (res); } token = strtok(NULL, ","); } efree(tempMailTo); } /* Send mail to all Cc rcpt's */ else if (headers && (pos1 = strstr(headers_lc, "cc:")) && ((pos1 == headers_lc) || (*(pos1-1) == '\n'))) { /* Real offset is memaddress from the original headers + difference of * string found in the lowercase headrs + 3 characters to jump over * the cc: */ pos1 = headers + (pos1 - headers_lc) + 3; if (NULL == (pos2 = strstr(pos1, "\r\n"))) { tempMailTo = estrndup(pos1, strlen(pos1)); } else { tempMailTo = estrndup(pos1, pos2 - pos1); } token = strtok(tempMailTo, ","); while (token != NULL) { SMTP_SKIP_SPACE(token); FormatEmailAddress(PW32G(mail_buffer), token, "RCPT TO:<%s>\r\n"); if ((res = Post(PW32G(mail_buffer))) != SUCCESS) { efree(tempMailTo); return (res); } if ((res = Ack(&server_response)) != SUCCESS) { SMTP_ERROR_RESPONSE(server_response); efree(tempMailTo); return (res); } token = strtok(NULL, ","); } efree(tempMailTo); } /* Send mail to all Bcc rcpt's This is basically a rip of the Cc code above. Just don't forget to remove the Bcc: from the header afterwards. */ if (mailBcc && *mailBcc) { tempMailTo = estrdup(mailBcc); /* Send mail to all rcpt's */ token = strtok(tempMailTo, ","); while (token != NULL) { SMTP_SKIP_SPACE(token); FormatEmailAddress(PW32G(mail_buffer), token, "RCPT TO:<%s>\r\n"); if ((res = Post(PW32G(mail_buffer))) != SUCCESS) { efree(tempMailTo); return (res); } if ((res = Ack(&server_response)) != SUCCESS) { SMTP_ERROR_RESPONSE(server_response); efree(tempMailTo); return (res); } token = strtok(NULL, ","); } efree(tempMailTo); } else if (headers) { if ((pos1 = strstr(headers_lc, "bcc:")) && (pos1 == headers_lc || *(pos1-1) == '\n')) { /* Real offset is memaddress from the original headers + difference of * string found in the lowercase headrs + 4 characters to jump over * the bcc: */ pos1 = headers + (pos1 - headers_lc) + 4; if (NULL == (pos2 = strstr(pos1, "\r\n"))) { tempMailTo = estrndup(pos1, strlen(pos1)); /* Later, when we remove the Bcc: out of the header we know it was the last thing. */ pos2 = pos1; } else { tempMailTo = estrndup(pos1, pos2 - pos1); } token = strtok(tempMailTo, ","); while (token != NULL) { SMTP_SKIP_SPACE(token); FormatEmailAddress(PW32G(mail_buffer), token, "RCPT TO:<%s>\r\n"); if ((res = Post(PW32G(mail_buffer))) != SUCCESS) { efree(tempMailTo); return (res); } if ((res = Ack(&server_response)) != SUCCESS) { SMTP_ERROR_RESPONSE(server_response); efree(tempMailTo); return (res); } token = strtok(NULL, ","); } efree(tempMailTo); /* Now that we've identified that we've a Bcc list, remove it from the current header. */ stripped_header = ecalloc(1, strlen(headers)); /* headers = point to string start of header pos1 = pointer IN headers where the Bcc starts '4' = Length of the characters 'bcc:' Because we've added +4 above for parsing the Emails we've to subtract them here. */ memcpy(stripped_header, headers, pos1 - headers - 4); if (pos1 != pos2) { /* if pos1 != pos2 , pos2 points to the rest of the headers. Since pos1 != pos2 if "\r\n" was found, we know those characters are there and so we jump over them (else we would generate a new header which would look like "\r\n\r\n". */ memcpy(stripped_header + (pos1 - headers - 4), pos2 + 2, strlen(pos2) - 2); } } } /* Simplify the code that we create a copy of stripped_header no matter if we actually strip something or not. So we've a single efree() later. */ if (headers && !stripped_header) { stripped_header = estrndup(headers, strlen(headers)); } if ((res = Post("DATA\r\n")) != SUCCESS) { if (stripped_header) { efree(stripped_header); } return (res); } if ((res = Ack(&server_response)) != SUCCESS) { SMTP_ERROR_RESPONSE(server_response); if (stripped_header) { efree(stripped_header); } return (res); } /* send message header */ if (Subject == NULL) { res = PostHeader(RPath, "No Subject", mailTo, stripped_header); } else { res = PostHeader(RPath, Subject, mailTo, stripped_header); } if (stripped_header) { efree(stripped_header); } if (res != SUCCESS) { return (res); } /* Escape \n. sequences * We use php_str_to_str() and not php_str_replace_in_subject(), since the latter * uses ZVAL as it's parameters */ data_cln = php_str_to_str(data, strlen(data), PHP_WIN32_MAIL_DOT_PATTERN, sizeof(PHP_WIN32_MAIL_DOT_PATTERN) - 1, PHP_WIN32_MAIL_DOT_REPLACE, sizeof(PHP_WIN32_MAIL_DOT_REPLACE) - 1); if (!data_cln) { data_cln = ZSTR_EMPTY_ALLOC(); } /* send message contents in 1024 chunks */ { char c, *e2, *e = ZSTR_VAL(data_cln) + ZSTR_LEN(data_cln); p = ZSTR_VAL(data_cln); while (e - p > 1024) { e2 = p + 1024; c = *e2; *e2 = '\0'; if ((res = Post(p)) != SUCCESS) { zend_string_free(data_cln); return(res); } *e2 = c; p = e2; } if ((res = Post(p)) != SUCCESS) { zend_string_free(data_cln); return(res); } } zend_string_free(data_cln); /*send termination dot */ if ((res = Post("\r\n.\r\n")) != SUCCESS) return (res); if ((res = Ack(&server_response)) != SUCCESS) { SMTP_ERROR_RESPONSE(server_response); return (res); } return (SUCCESS); }
/********************************************************************* // Name: TSendMail // Input: 1) host: Name of the mail host where the SMTP server resides // max accepted length of name = 256 // 2) appname: Name of the application to use in the X-mailer // field of the message. if NULL is given the application // name is used as given by the GetCommandLine() function // max accespted length of name = 100 // Output: 1) error: Returns the error code if something went wrong or // SUCCESS otherwise. // // See SendText() for additional args! //********************************************************************/ PHPAPI int TSendMail(char *host, int *error, char **error_message, char *headers, char *Subject, char *mailTo, char *data, char *mailCc, char *mailBcc, char *mailRPath) { int ret; char *RPath = NULL; zend_string *headers_lc = NULL, *headers_trim = NULL; /* headers_lc is only created if we've a header at all */ char *pos1 = NULL, *pos2 = NULL; if (host == NULL) { *error = BAD_MAIL_HOST; return FAILURE; } else if (strlen(host) >= HOST_NAME_LEN) { *error = BAD_MAIL_HOST; return FAILURE; } else { strcpy(PW32G(mail_host), host); } if (headers) { char *pos = NULL; /* Use PCRE to trim the header into the right format */ if (NULL == (headers_trim = php_win32_mail_trim_header(headers))) { *error = W32_SM_PCRE_ERROR; return FAILURE; } /* Create a lowercased header for all the searches so we're finally case * insensitive when searching for a pattern. */ headers_lc = zend_string_tolower(headers_trim); } /* Fall back to sendmail_from php.ini setting */ if (mailRPath && *mailRPath) { RPath = estrdup(mailRPath); } else if (INI_STR("sendmail_from")) { RPath = estrdup(INI_STR("sendmail_from")); } else if (headers_lc) { int found = 0; char *lookup = ZSTR_VAL(headers_lc); while (lookup) { pos1 = strstr(lookup, "from:"); if (!pos1) { break; } else if (pos1 != ZSTR_VAL(headers_lc) && *(pos1-1) != '\n') { if (strlen(pos1) >= sizeof("from:")) { lookup = pos1 + sizeof("from:"); continue; } else { break; } } found = 1; /* Real offset is memaddress from the original headers + difference of * string found in the lowercase headrs + 5 characters to jump over * the from: */ pos1 = headers + (pos1 - lookup) + 5; if (NULL == (pos2 = strstr(pos1, "\r\n"))) { RPath = estrndup(pos1, strlen(pos1)); } else { RPath = estrndup(pos1, pos2 - pos1); } break; } if (!found) { if (headers_lc) { zend_string_free(headers_lc); } *error = W32_SM_SENDMAIL_FROM_NOT_SET; return FAILURE; } } /* attempt to connect with mail host */ *error = MailConnect(); if (*error != 0) { if (RPath) { efree(RPath); } if (headers) { zend_string_free(headers_trim); zend_string_free(headers_lc); } /* 128 is safe here, the specifier in snprintf isn't longer than that */ *error_message = ecalloc(1, HOST_NAME_LEN + 128); snprintf(*error_message, HOST_NAME_LEN + 128, "Failed to connect to mailserver at \"%s\" port %d, verify your \"SMTP\" " "and \"smtp_port\" setting in php.ini or use ini_set()", PW32G(mail_host), !INI_INT("smtp_port") ? 25 : INI_INT("smtp_port")); return FAILURE; } else { ret = SendText(RPath, Subject, mailTo, mailCc, mailBcc, data, headers ? ZSTR_VAL(headers_trim) : NULL, headers ? ZSTR_VAL(headers_lc) : NULL, error_message); TSMClose(); if (RPath) { efree(RPath); } if (headers) { zend_string_free(headers_trim); zend_string_free(headers_lc); } if (ret != SUCCESS) { *error = ret; return FAILURE; } return SUCCESS; } }
//******************************************************************** // Name: TSendText // Input: 1) RPath: return path of the message // Is used to fill the "Return-Path" and the // "X-Sender" fields of the message. // 2) Subject: Subject field of the message. If NULL is given // the subject is set to "No Subject" // 3) mailTo: Destination address // 4) data: Null terminated string containing the data to be send. // Output: Error code or SUCCESS // Description: // Author/Date: jcar 20/9/96 // History: //******************************************************************** int SendText(char *RPath, char *Subject, char *mailTo, char *data, char *headers) { int res, i; char *p; char *tempMailTo, *token, *pos1, *pos2; TLS_VARS; // check for NULL parameters if (data == NULL) return (BAD_MSG_CONTENTS); if (mailTo == NULL) return (BAD_MSG_DESTINATION); if (RPath == NULL) return (BAD_MSG_RPATH); // simple checks for the mailto address // have ampersand ? if (strchr(mailTo, '@') == NULL) return (BAD_MSG_DESTINATION); sprintf(GLOBAL(Buffer), "HELO %s\r\n", GLOBAL(LocalHost)); // in the beggining of the dialog // attempt reconnect if the first Post fail if ((res = Post(GLOBAL(Buffer))) != SUCCESS) { MailConnect(); if ((res = Post(GLOBAL(Buffer))) != SUCCESS) return (res); } if ((res = Ack()) != SUCCESS) return (res); sprintf(GLOBAL(Buffer), "MAIL FROM:<%s>\r\n", RPath); if ((res = Post(GLOBAL(Buffer))) != SUCCESS) return (res); if ((res = Ack()) != SUCCESS) return (res); tempMailTo = estrdup(mailTo); // Send mail to all rcpt's token = strtok(tempMailTo, ","); while(token != NULL) { sprintf(GLOBAL(Buffer), "RCPT TO:<%s>\r\n", token); if ((res = Post(GLOBAL(Buffer))) != SUCCESS) return (res); if ((res = Ack()) != SUCCESS) return (res); token = strtok(NULL, ","); } // Send mail to all Cc rcpt's efree(tempMailTo); if (headers && (pos1 = strstr(headers, "Cc:"))) { pos2 = strstr(pos1, "\r\n"); tempMailTo = estrndup(pos1, pos2-pos1); token = strtok(tempMailTo, ","); while(token != NULL) { sprintf(GLOBAL(Buffer), "RCPT TO:<%s>\r\n", token); if ((res = Post(GLOBAL(Buffer))) != SUCCESS) return (res); if ((res = Ack()) != SUCCESS) return (res); token = strtok(NULL, ","); } efree(tempMailTo); } if ((res = Post("DATA\r\n")) != SUCCESS) return (res); if ((res = Ack()) != SUCCESS) return (res); // send message header if (Subject == NULL) res = PostHeader(RPath, "No Subject", mailTo, headers); else res = PostHeader(RPath, Subject, mailTo, headers); if (res != SUCCESS) return (res); // send message contents in 1024 chunks if (strlen(data) <= 1024) { if ((res = Post(data)) != SUCCESS) return (res); } else { p = data; while (1) { if (*p == '\0') break; if (strlen(p) >= 1024) i = 1024; else i = strlen(p); // put next chunk in buffer strncpy(GLOBAL(Buffer), p, i); GLOBAL(Buffer)[i] = '\0'; p += i; // send chunk if ((res = Post(GLOBAL(Buffer))) != SUCCESS) return (res); } } //send termination dot if ((res = Post("\r\n.\r\n")) != SUCCESS) return (res); if ((res = Ack()) != SUCCESS) return (res); return (SUCCESS); }
/********************************************************************* // Name: TSendMail // Input: 1) host: Name of the mail host where the SMTP server resides // max accepted length of name = 256 // 2) appname: Name of the application to use in the X-mailer // field of the message. if NULL is given the application // name is used as given by the GetCommandLine() function // max accespted length of name = 100 // Output: 1) error: Returns the error code if something went wrong or // SUCCESS otherwise. // // See SendText() for additional args! //********************************************************************/ PHPAPI int TSendMail(char *host, int *error, char **error_message, char *headers, char *Subject, char *mailTo, char *data, char *mailCc, char *mailBcc, char *mailRPath) { int ret; char *RPath = NULL; zend_string *headers_lc = NULL; /* headers_lc is only created if we've a header at all */ char *pos1 = NULL, *pos2 = NULL; #ifndef NETWARE WinsockStarted = FALSE; #endif if (host == NULL) { *error = BAD_MAIL_HOST; return FAILURE; } else if (strlen(host) >= HOST_NAME_LEN) { *error = BAD_MAIL_HOST; return FAILURE; } else { strcpy(MailHost, host); } if (headers) { char *pos = NULL; size_t i; zend_string *headers_trim; /* Use PCRE to trim the header into the right format */ if (NULL == (headers_trim = php_win32_mail_trim_header(headers))) { *error = W32_SM_PCRE_ERROR; return FAILURE; } /* Create a lowercased header for all the searches so we're finally case * insensitive when searching for a pattern. */ if (NULL == (headers_lc = zend_string_copy(headers_trim))) { *error = OUT_OF_MEMORY; return FAILURE; } for (i = 0; i < headers_lc->len; i++) { headers_lc->val[i] = tolower(headers_lc->val[i]); } } /* Fall back to sendmail_from php.ini setting */ if (mailRPath && *mailRPath) { RPath = estrdup(mailRPath); } else if (INI_STR("sendmail_from")) { RPath = estrdup(INI_STR("sendmail_from")); } else if ( headers_lc && (pos1 = strstr(headers_lc->val, "from:")) && ((pos1 == headers_lc->val) || (*(pos1-1) == '\n')) ) { /* Real offset is memaddress from the original headers + difference of * string found in the lowercase headrs + 5 characters to jump over * the from: */ pos1 = headers + (pos1 - headers_lc->val) + 5; if (NULL == (pos2 = strstr(pos1, "\r\n"))) { RPath = estrndup(pos1, strlen(pos1)); } else { RPath = estrndup(pos1, pos2 - pos1); } } else { if (headers_lc) { zend_string_free(headers_lc); } *error = W32_SM_SENDMAIL_FROM_NOT_SET; return FAILURE; } /* attempt to connect with mail host */ *error = MailConnect(); if (*error != 0) { if (RPath) { efree(RPath); } if (headers) { efree(headers); efree(headers_lc); } /* 128 is safe here, the specifier in snprintf isn't longer than that */ if (NULL == (*error_message = ecalloc(1, HOST_NAME_LEN + 128))) { return FAILURE; } snprintf(*error_message, HOST_NAME_LEN + 128, "Failed to connect to mailserver at \"%s\" port %d, verify your \"SMTP\" " "and \"smtp_port\" setting in php.ini or use ini_set()", MailHost, !INI_INT("smtp_port") ? 25 : INI_INT("smtp_port")); return FAILURE; } else { ret = SendText(RPath, Subject, mailTo, mailCc, mailBcc, data, headers, headers_lc->val, error_message); TSMClose(); if (RPath) { efree(RPath); } if (headers) { efree(headers); efree(headers_lc); } if (ret != SUCCESS) { *error = ret; return FAILURE; } return SUCCESS; } }