#include <winsock.h>
#include "time.h"
#include <string.h>
+#include <math.h>
#include <malloc.h>
#include <memory.h>
#include <winbase.h>
#include "ext/pcre/php_pcre.h"
#endif
+#include "ext/standard/php_string.h"
+
/*
extern int _daylight;
extern long _timezone;
efree(response); \
} \
}
+#define SMTP_SKIP_SPACE(str) { while (isspace(*str)) { str++; } }
+
#ifndef THREAD_SAFE
char Buffer[MAIL_BUFFER_SIZE];
#define PHP_WIN32_MAIL_RMVDBL_PATTERN "/^\r\n|(\r\n)+$/m"
#define PHP_WIN32_MAIL_RMVDBL_REPLACE ""
+/* This pattern escapes \n. inside the message body. It prevents
+ * premature end of message if \n.\n or \r\n.\r\n is encountered
+ * and ensures that \n. sequences are properly displayed in the
+ * message body. */
+#define PHP_WIN32_MAIL_DOT_PATTERN "\n."
+#define PHP_WIN32_MAIL_DOT_REPLACE "\n.."
+
/* This function is meant to unify the headers passed to to mail()
* This means, use PCRE to transform single occurences of \n or \r in \r\n
* As a second step we also eleminate all \r\n occurences which are:
char *tempMailTo, *token, *pos1, *pos2;
char *server_response = NULL;
char *stripped_header = NULL;
+ char *data_cln;
+ int data_cln_len;
/* check for NULL parameters */
if (data == NULL)
token = strtok(tempMailTo, ",");
while(token != NULL)
{
+ SMTP_SKIP_SPACE(token);
sprintf(Buffer, "RCPT TO:<%s>\r\n", token);
if ((res = Post(Buffer)) != SUCCESS)
return (res);
token = strtok(tempMailTo, ",");
while(token != NULL)
{
+ SMTP_SKIP_SPACE(token);
snprintf(Buffer, MAIL_BUFFER_SIZE, "RCPT TO:<%s>\r\n", token);
if ((res = Post(Buffer)) != SUCCESS) {
efree(tempMailTo);
token = strtok(tempMailTo, ",");
while(token != NULL)
{
+ SMTP_SKIP_SPACE(token);
sprintf(Buffer, "RCPT TO:<%s>\r\n", token);
if ((res = Post(Buffer)) != SUCCESS) {
return (res);
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, &data_cln_len);
+
/* send message contents in 1024 chunks */
- if (strlen(data) <= 1024) {
- if ((res = Post(data)) != SUCCESS)
+ if (data_cln_len <= 1024) {
+ if ((res = Post(data_cln)) != SUCCESS) {
+ efree(data_cln);
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(Buffer, p, i);
- Buffer[i] = '\0';
- p += i;
-
+ int parts = (int) floor(data_cln_len / 1024);
+ p = data_cln;
+
+ for (i = 0; i < parts; i++) {
+ strlcpy(Buffer, p, 1024);
+ Buffer[1024] = '\0';
+ p += 1024;
+send_chunk:
/* send chunk */
- if ((res = Post(Buffer)) != SUCCESS)
+ if ((res = Post(Buffer)) != SUCCESS) {
+ efree(data_cln);
return (res);
+ }
+ }
+
+ if ((parts * 1024) < data_cln_len) {
+ i = data_cln_len - (parts * 1024);
+ strlcpy(Buffer, p, i);
+ Buffer[i] = '\0';
+ goto send_chunk;
}
}
+ efree(data_cln);
+
/*send termination dot */
if ((res = Post("\r\n.\r\n")) != SUCCESS)
return (res);