Skip to content

Commit 708144b

Browse files
committed
win32/sendmail.c: move initial MailConnect() into SendText()
As we don't do anything with it prior to it and we already try to reconect in SendText()
1 parent aab2c2b commit 708144b

File tree

1 file changed

+34
-48
lines changed

1 file changed

+34
-48
lines changed

win32/sendmail.c

Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ static const char *ErrorMessages[] =
112112
#define PHP_WIN32_MAIL_DOT_PATTERN "\n."
113113
#define PHP_WIN32_MAIL_DOT_REPLACE "\n.."
114114

115-
static int SendText(const char *RPath, const char *Subject, const char *mailTo, const char *data,
115+
static int SendText(_In_ const char *host, const char *RPath, const char *Subject, const char *mailTo, const char *data,
116116
zend_string *headers, zend_string *headers_lc, char **error_message);
117117
static int MailConnect();
118118
static bool PostHeader(const char *RPath, const char *Subject, const char *mailTo, zend_string *xheaders);
@@ -196,11 +196,6 @@ PHPAPI int TSendMail(const char *host, int *error, char **error_message,
196196
if (host == NULL) {
197197
*error = BAD_MAIL_HOST;
198198
return FAILURE;
199-
} else if (strlen(host) >= HOST_NAME_LEN) {
200-
*error = BAD_MAIL_HOST;
201-
return FAILURE;
202-
} else {
203-
strcpy(PW32G(mail_host), host);
204199
}
205200

206201
if (headers) {
@@ -261,39 +256,20 @@ PHPAPI int TSendMail(const char *host, int *error, char **error_message,
261256
}
262257
}
263258

264-
/* attempt to connect with mail host */
265-
*error = MailConnect();
266-
if (*error != 0) {
267-
if (RPath) {
268-
efree(RPath);
269-
}
270-
if (headers) {
271-
zend_string_release(headers_trim);
272-
zend_string_release(headers_lc);
273-
}
274-
/* 128 is safe here, the specifier in snprintf isn't longer than that */
275-
*error_message = ecalloc(1, HOST_NAME_LEN + 128);
276-
snprintf(*error_message, HOST_NAME_LEN + 128,
277-
"Failed to connect to mailserver at \"%s\" port " ZEND_ULONG_FMT ", verify your \"SMTP\" "
278-
"and \"smtp_port\" setting in php.ini or use ini_set()",
279-
PW32G(mail_host), !INI_INT("smtp_port") ? 25 : INI_INT("smtp_port"));
259+
ret = SendText(host, RPath, Subject, mailTo, data, headers_trim, headers_lc, error_message);
260+
TSMClose();
261+
if (RPath) {
262+
efree(RPath);
263+
}
264+
if (headers) {
265+
zend_string_release(headers_trim);
266+
zend_string_release(headers_lc);
267+
}
268+
if (ret != SUCCESS) {
269+
*error = ret;
280270
return FAILURE;
281-
} else {
282-
ret = SendText(RPath, Subject, mailTo, data, headers_trim, headers_lc, error_message);
283-
TSMClose();
284-
if (RPath) {
285-
efree(RPath);
286-
}
287-
if (headers) {
288-
zend_string_release(headers_trim);
289-
zend_string_release(headers_lc);
290-
}
291-
if (ret != SUCCESS) {
292-
*error = ret;
293-
return FAILURE;
294-
}
295-
return SUCCESS;
296271
}
272+
return SUCCESS;
297273
}
298274

299275
//*********************************************************************
@@ -387,7 +363,7 @@ static char *find_address(char *list, char **state)
387363
// Author/Date: jcar 20/9/96
388364
// History:
389365
//*********************************************************************
390-
static int SendText(const char *RPath, const char *Subject, const char *mailTo, const char *data,
366+
static int SendText(_In_ const char *host, const char *RPath, const char *Subject, const char *mailTo, const char *data,
391367
zend_string *headers, zend_string *headers_lc, char **error_message)
392368
{
393369
int res;
@@ -415,19 +391,29 @@ static int SendText(const char *RPath, const char *Subject, const char *mailTo,
415391
return (BAD_MSG_DESTINATION);
416392
*/
417393

394+
if (strlen(host) >= HOST_NAME_LEN) {
395+
*error = BAD_MAIL_HOST;
396+
return FAILURE;
397+
} else {
398+
strcpy(PW32G(mail_host), host);
399+
}
400+
401+
/* attempt to connect with mail host */
402+
res = MailConnect();
403+
if (res != 0) {
404+
/* 128 is safe here, the specifier in snprintf isn't longer than that */
405+
*error_message = ecalloc(1, HOST_NAME_LEN + 128);
406+
snprintf(*error_message, HOST_NAME_LEN + 128,
407+
"Failed to connect to mailserver at \"%s\" port " ZEND_ULONG_FMT ", verify your \"SMTP\" "
408+
"and \"smtp_port\" setting in php.ini or use ini_set()",
409+
host, !INI_INT("smtp_port") ? 25 : INI_INT("smtp_port"));
410+
return res;
411+
}
412+
418413
snprintf(PW32G(mail_buffer), sizeof(PW32G(mail_buffer)), "HELO %s\r\n", PW32G(mail_local_host));
419414

420-
/* in the beginning of the dialog */
421-
/* attempt reconnect if the first Post fail */
422415
if (!Post(PW32G(mail_buffer))) {
423-
int err = MailConnect();
424-
if (0 != err) {
425-
return (FAILED_TO_SEND);
426-
}
427-
428-
if (!Post(PW32G(mail_buffer))) {
429-
return (FAILED_TO_SEND);
430-
}
416+
return (FAILED_TO_SEND);
431417
}
432418
if ((res = Ack(&server_response)) != SUCCESS) {
433419
SMTP_ERROR_RESPONSE(server_response);

0 commit comments

Comments
 (0)