//******************************************************************** // 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: 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: 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; } }