2019-07-27 15:47:32 +08:00
|
|
|
/*
|
1999-04-08 05:05:13 +08:00
|
|
|
* PHP Sendmail for Windows.
|
|
|
|
*
|
2020-01-16 16:46:47 +08:00
|
|
|
* This file is rewritten specifically for PHPFI. Some functionality
|
2015-01-03 17:22:58 +08:00
|
|
|
* has been removed (MIME and file attachments). This code was
|
2014-11-20 03:59:31 +08:00
|
|
|
* modified from code based on code written by Jarle Aase.
|
1999-04-08 05:05:13 +08:00
|
|
|
*
|
2018-08-10 10:19:55 +08:00
|
|
|
* This class is based on the original code by Jarle Aase, see below:
|
1999-04-08 05:05:13 +08:00
|
|
|
* wSendmail.cpp It has been striped of some functionality to match
|
|
|
|
* the requirements of phpfi.
|
|
|
|
*
|
|
|
|
* Very simple SMTP Send-mail program for sending command-line level
|
|
|
|
* emails and CGI-BIN form response for the Windows platform.
|
|
|
|
*
|
2000-08-04 12:49:45 +08:00
|
|
|
* The complete wSendmail package with source code can be located
|
|
|
|
* from http://www.jgaa.com
|
1999-04-08 05:05:13 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "php.h" /*php specific */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2003-02-16 11:48:49 +08:00
|
|
|
#include <winsock2.h>
|
2005-02-25 13:57:41 +08:00
|
|
|
#include "time.h"
|
2012-03-04 23:08:12 +08:00
|
|
|
# include <Ws2tcpip.h>
|
1999-04-08 05:05:13 +08:00
|
|
|
#include <string.h>
|
2003-07-24 00:03:10 +08:00
|
|
|
#include <math.h>
|
1999-04-08 05:05:13 +08:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <winbase.h>
|
|
|
|
#include "sendmail.h"
|
1999-04-10 21:32:47 +08:00
|
|
|
#include "php_ini.h"
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2015-03-04 03:42:28 +08:00
|
|
|
#include "php_win32_globals.h"
|
|
|
|
|
2002-06-06 01:08:38 +08:00
|
|
|
#include "ext/pcre/php_pcre.h"
|
2003-07-24 00:03:10 +08:00
|
|
|
#include "ext/standard/php_string.h"
|
2005-08-08 06:06:29 +08:00
|
|
|
#include "ext/date/php_date.h"
|
2003-07-24 00:03:10 +08:00
|
|
|
|
2017-02-01 01:25:36 +08:00
|
|
|
#define SENDMAIL_DEBUG 0
|
|
|
|
|
1999-04-08 05:05:13 +08:00
|
|
|
/*enum
|
|
|
|
{
|
|
|
|
DO_CONNECT = WM_USER +1
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
2002-05-15 04:40:39 +08:00
|
|
|
/* '*error_message' has to be passed around from php_mail() */
|
|
|
|
#define SMTP_ERROR_RESPONSE_SPEC "SMTP server response: %s"
|
2018-08-10 10:19:55 +08:00
|
|
|
/* Convenient way to handle error messages from the SMTP server.
|
2002-05-15 04:40:39 +08:00
|
|
|
response is ecalloc()d in Ack() itself and efree()d here
|
|
|
|
because the content is in *error_message now */
|
|
|
|
#define SMTP_ERROR_RESPONSE(response) { \
|
2002-07-03 07:44:06 +08:00
|
|
|
if (response && error_message) { \
|
2017-03-13 00:19:20 +08:00
|
|
|
*error_message = ecalloc(1, sizeof(SMTP_ERROR_RESPONSE_SPEC) + strlen(response)); \
|
|
|
|
snprintf(*error_message, sizeof(SMTP_ERROR_RESPONSE_SPEC) + strlen(response), SMTP_ERROR_RESPONSE_SPEC, response); \
|
2002-07-03 07:44:06 +08:00
|
|
|
efree(response); \
|
2002-05-15 04:40:39 +08:00
|
|
|
} \
|
|
|
|
}
|
2003-07-24 00:03:10 +08:00
|
|
|
#define SMTP_SKIP_SPACE(str) { while (isspace(*str)) { str++; } }
|
|
|
|
|
2002-05-15 04:40:39 +08:00
|
|
|
|
1999-04-08 05:05:13 +08:00
|
|
|
char seps[] = " ,\t\n";
|
2014-09-20 09:27:40 +08:00
|
|
|
char *php_mailer = "PHP 7 WIN32";
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2000-06-17 02:24:02 +08:00
|
|
|
/* Error messages */
|
1999-04-08 05:05:13 +08:00
|
|
|
static char *ErrorMessages[] =
|
|
|
|
{
|
2002-05-14 21:52:18 +08:00
|
|
|
{"Success"}, /* 0 */
|
|
|
|
{"Bad arguments from form"}, /* 1 */
|
1999-04-08 05:05:13 +08:00
|
|
|
{"Unable to open temporary mailfile for read"},
|
|
|
|
{"Failed to Start Sockets"},
|
|
|
|
{"Failed to Resolve Host"},
|
2002-05-14 21:52:18 +08:00
|
|
|
{"Failed to obtain socket handle"}, /* 5 */
|
|
|
|
{"Failed to connect to mailserver, verify your \"SMTP\" setting in php.ini"},
|
1999-04-08 05:05:13 +08:00
|
|
|
{"Failed to Send"},
|
|
|
|
{"Failed to Receive"},
|
|
|
|
{"Server Error"},
|
2002-05-14 21:52:18 +08:00
|
|
|
{"Failed to resolve the host IP name"}, /* 10 */
|
1999-04-08 05:05:13 +08:00
|
|
|
{"Out of memory"},
|
|
|
|
{"Unknown error"},
|
|
|
|
{"Bad Message Contents"},
|
|
|
|
{"Bad Message Subject"},
|
2002-05-14 21:52:18 +08:00
|
|
|
{"Bad Message destination"}, /* 15 */
|
1999-04-08 05:05:13 +08:00
|
|
|
{"Bad Message Return Path"},
|
|
|
|
{"Bad Mail Host"},
|
|
|
|
{"Bad Message File"},
|
2002-05-15 04:40:39 +08:00
|
|
|
{"\"sendmail_from\" not set in php.ini or custom \"From:\" header missing"},
|
2002-06-06 01:08:38 +08:00
|
|
|
{"Mailserver rejected our \"sendmail_from\" setting"}, /* 20 */
|
2022-03-30 17:30:11 +08:00
|
|
|
{"Error while trimming mail header with PCRE, please file a bug report at https://github.com/php/php-src/issues"} /* 21 */
|
1999-04-08 05:05:13 +08:00
|
|
|
};
|
|
|
|
|
2013-06-11 05:20:18 +08:00
|
|
|
/* This pattern converts all single occurrences of \n (Unix)
|
2020-01-17 02:04:42 +08:00
|
|
|
* without a leading \r to \r\n and all occurrences of \r (Mac)
|
2002-06-06 01:08:38 +08:00
|
|
|
* without a trailing \n to \r\n
|
|
|
|
* Thx to Nibbler from ircnet/#linuxger
|
|
|
|
*/
|
|
|
|
#define PHP_WIN32_MAIL_UNIFY_PATTERN "/(\r\n?)|\n/"
|
|
|
|
#define PHP_WIN32_MAIL_UNIFY_REPLACE "\r\n"
|
|
|
|
|
|
|
|
/* This pattern removes \r\n from the start of the string,
|
|
|
|
* \r\n from the end of the string and also makes sure every line
|
|
|
|
* is only wrapped with a single \r\n (thus reduces multiple
|
2013-06-11 05:20:18 +08:00
|
|
|
* occurrences of \r\n between lines to a single \r\n) */
|
2002-06-06 01:08:38 +08:00
|
|
|
#define PHP_WIN32_MAIL_RMVDBL_PATTERN "/^\r\n|(\r\n)+$/m"
|
|
|
|
#define PHP_WIN32_MAIL_RMVDBL_REPLACE ""
|
|
|
|
|
2003-07-24 00:03:10 +08:00
|
|
|
/* 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.."
|
|
|
|
|
2002-06-06 01:08:38 +08:00
|
|
|
/* This function is meant to unify the headers passed to to mail()
|
2013-06-11 05:20:18 +08:00
|
|
|
* This means, use PCRE to transform single occurrences of \n or \r in \r\n
|
2020-01-16 19:04:00 +08:00
|
|
|
* As a second step we also eliminate all \r\n occurrences which are:
|
2002-06-06 01:08:38 +08:00
|
|
|
* 1) At the start of the header
|
|
|
|
* 2) At the end of the header
|
2013-06-11 05:20:18 +08:00
|
|
|
* 3) Two or more occurrences in the header are removed so only one is left
|
2002-06-06 01:08:38 +08:00
|
|
|
*
|
|
|
|
* Returns NULL on error, or the new char* buffer on success.
|
|
|
|
* You have to take care and efree() the buffer on your own.
|
|
|
|
*/
|
2020-06-07 17:01:19 +08:00
|
|
|
static zend_string *php_win32_mail_trim_header(const char *header)
|
2002-06-06 01:08:38 +08:00
|
|
|
{
|
2014-05-13 12:51:11 +08:00
|
|
|
zend_string *result, *result2;
|
2017-06-07 18:50:16 +08:00
|
|
|
zend_string *replace;
|
2014-05-13 12:51:11 +08:00
|
|
|
zend_string *regex;
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2002-06-06 01:08:38 +08:00
|
|
|
if (!header) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-06-07 18:50:16 +08:00
|
|
|
replace = zend_string_init(PHP_WIN32_MAIL_UNIFY_REPLACE, strlen(PHP_WIN32_MAIL_UNIFY_REPLACE), 0);
|
2015-01-13 19:53:00 +08:00
|
|
|
regex = zend_string_init(PHP_WIN32_MAIL_UNIFY_PATTERN, sizeof(PHP_WIN32_MAIL_UNIFY_PATTERN)-1, 0);
|
2002-06-06 01:08:38 +08:00
|
|
|
|
2014-05-13 12:51:11 +08:00
|
|
|
result = php_pcre_replace(regex,
|
2017-10-12 18:48:36 +08:00
|
|
|
NULL, header, strlen(header),
|
2017-06-07 18:50:16 +08:00
|
|
|
replace,
|
2015-01-15 22:16:25 +08:00
|
|
|
-1,
|
|
|
|
NULL);
|
2014-05-13 12:51:11 +08:00
|
|
|
|
2018-05-28 21:27:12 +08:00
|
|
|
zend_string_release_ex(replace, 0);
|
|
|
|
zend_string_release_ex(regex, 0);
|
2015-02-10 18:30:25 +08:00
|
|
|
|
2002-06-06 01:08:38 +08:00
|
|
|
if (NULL == result) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-06-07 18:50:16 +08:00
|
|
|
replace = zend_string_init(PHP_WIN32_MAIL_RMVDBL_PATTERN, strlen(PHP_WIN32_MAIL_RMVDBL_PATTERN), 0);
|
2014-08-26 01:24:55 +08:00
|
|
|
regex = zend_string_init(PHP_WIN32_MAIL_RMVDBL_PATTERN, sizeof(PHP_WIN32_MAIL_RMVDBL_PATTERN)-1, 0);
|
2002-06-06 01:08:38 +08:00
|
|
|
|
2014-05-13 12:51:11 +08:00
|
|
|
result2 = php_pcre_replace(regex,
|
2017-10-12 18:48:36 +08:00
|
|
|
result, ZSTR_VAL(result), ZSTR_LEN(result),
|
2017-06-07 18:50:16 +08:00
|
|
|
replace,
|
2015-01-15 22:16:25 +08:00
|
|
|
-1,
|
|
|
|
NULL);
|
2018-05-28 21:27:12 +08:00
|
|
|
zend_string_release_ex(replace, 0);
|
|
|
|
zend_string_release_ex(regex, 0);
|
|
|
|
zend_string_release_ex(result, 0);
|
2015-02-10 18:30:25 +08:00
|
|
|
|
|
|
|
return result2;
|
2002-06-06 01:08:38 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2024-08-10 19:59:06 +08:00
|
|
|
//*********************************************************************
|
1999-04-08 05:05:13 +08:00
|
|
|
// 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
|
2021-03-31 11:49:41 +08:00
|
|
|
// max accepted length of name = 100
|
1999-04-08 05:05:13 +08:00
|
|
|
// Output: 1) error: Returns the error code if something went wrong or
|
|
|
|
// SUCCESS otherwise.
|
|
|
|
//
|
|
|
|
// See SendText() for additional args!
|
2024-08-10 19:59:06 +08:00
|
|
|
//*********************************************************************
|
2020-06-07 17:01:19 +08:00
|
|
|
PHPAPI int TSendMail(const char *host, int *error, char **error_message,
|
|
|
|
const char *headers, const char *Subject, const char *mailTo, const char *data,
|
2014-12-14 06:06:14 +08:00
|
|
|
char *mailCc, char *mailBcc, char *mailRPath)
|
1999-04-08 05:05:13 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char *RPath = NULL;
|
2017-02-01 19:36:14 +08:00
|
|
|
zend_string *headers_lc = NULL, *headers_trim = NULL; /* headers_lc is only created if we've a header at all */
|
2020-06-07 17:01:19 +08:00
|
|
|
const char *pos1 = NULL, *pos2 = NULL;
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
if (host == NULL) {
|
|
|
|
*error = BAD_MAIL_HOST;
|
2002-05-15 04:40:39 +08:00
|
|
|
return FAILURE;
|
1999-04-08 05:05:13 +08:00
|
|
|
} else if (strlen(host) >= HOST_NAME_LEN) {
|
|
|
|
*error = BAD_MAIL_HOST;
|
2002-05-15 04:40:39 +08:00
|
|
|
return FAILURE;
|
1999-04-08 05:05:13 +08:00
|
|
|
} else {
|
2015-03-04 03:42:28 +08:00
|
|
|
strcpy(PW32G(mail_host), host);
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
|
|
|
|
2002-05-14 23:29:28 +08:00
|
|
|
if (headers) {
|
|
|
|
char *pos = NULL;
|
2002-06-06 01:08:38 +08:00
|
|
|
|
|
|
|
/* Use PCRE to trim the header into the right format */
|
2017-02-01 19:36:14 +08:00
|
|
|
if (NULL == (headers_trim = php_win32_mail_trim_header(headers))) {
|
2002-06-06 01:08:38 +08:00
|
|
|
*error = W32_SM_PCRE_ERROR;
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
2002-05-18 00:16:27 +08:00
|
|
|
/* Create a lowercased header for all the searches so we're finally case
|
|
|
|
* insensitive when searching for a pattern. */
|
2017-02-01 19:36:14 +08:00
|
|
|
headers_lc = zend_string_tolower(headers_trim);
|
2002-05-14 23:29:28 +08:00
|
|
|
}
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2002-05-14 23:29:28 +08:00
|
|
|
/* Fall back to sendmail_from php.ini setting */
|
2002-07-03 06:29:13 +08:00
|
|
|
if (mailRPath && *mailRPath) {
|
|
|
|
RPath = estrdup(mailRPath);
|
2005-01-20 01:38:20 +08:00
|
|
|
} else if (INI_STR("sendmail_from")) {
|
2002-06-03 06:54:48 +08:00
|
|
|
RPath = estrdup(INI_STR("sendmail_from"));
|
2015-08-19 17:05:35 +08:00
|
|
|
} else if (headers_lc) {
|
|
|
|
int found = 0;
|
2020-06-07 17:01:19 +08:00
|
|
|
const char *lookup = ZSTR_VAL(headers_lc);
|
2015-08-19 17:05:35 +08:00
|
|
|
|
|
|
|
while (lookup) {
|
|
|
|
pos1 = strstr(lookup, "from:");
|
|
|
|
|
|
|
|
if (!pos1) {
|
|
|
|
break;
|
2017-02-01 19:45:19 +08:00
|
|
|
} else if (pos1 != ZSTR_VAL(headers_lc) && *(pos1-1) != '\n') {
|
2015-08-19 17:05:35 +08:00
|
|
|
if (strlen(pos1) >= sizeof("from:")) {
|
|
|
|
lookup = pos1 + sizeof("from:");
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
found = 1;
|
|
|
|
|
|
|
|
/* Real offset is memaddress from the original headers + difference of
|
2021-03-31 11:49:41 +08:00
|
|
|
* string found in the lowercase headers + 5 characters to jump over
|
2015-08-19 17:05:35 +08:00
|
|
|
* 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;
|
2005-01-20 01:38:20 +08:00
|
|
|
}
|
2015-08-19 17:05:35 +08:00
|
|
|
|
|
|
|
if (!found) {
|
2019-03-29 18:12:09 +08:00
|
|
|
if (headers) {
|
|
|
|
zend_string_release(headers_trim);
|
|
|
|
zend_string_release(headers_lc);
|
2015-08-19 17:05:35 +08:00
|
|
|
}
|
|
|
|
*error = W32_SM_SENDMAIL_FROM_NOT_SET;
|
|
|
|
return FAILURE;
|
2002-05-14 23:29:28 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
|
|
|
|
2000-06-17 02:24:02 +08:00
|
|
|
/* attempt to connect with mail host */
|
1999-04-08 05:05:13 +08:00
|
|
|
*error = MailConnect();
|
|
|
|
if (*error != 0) {
|
2002-05-15 04:40:39 +08:00
|
|
|
if (RPath) {
|
|
|
|
efree(RPath);
|
|
|
|
}
|
2002-06-06 01:08:38 +08:00
|
|
|
if (headers) {
|
2019-03-29 18:12:09 +08:00
|
|
|
zend_string_release(headers_trim);
|
|
|
|
zend_string_release(headers_lc);
|
2002-05-18 00:16:27 +08:00
|
|
|
}
|
2002-05-15 18:11:33 +08:00
|
|
|
/* 128 is safe here, the specifier in snprintf isn't longer than that */
|
2017-03-13 00:19:20 +08:00
|
|
|
*error_message = ecalloc(1, HOST_NAME_LEN + 128);
|
2002-06-03 01:13:48 +08:00
|
|
|
snprintf(*error_message, HOST_NAME_LEN + 128,
|
2020-01-16 01:18:36 +08:00
|
|
|
"Failed to connect to mailserver at \"%s\" port " ZEND_ULONG_FMT ", verify your \"SMTP\" "
|
2002-06-03 01:13:48 +08:00
|
|
|
"and \"smtp_port\" setting in php.ini or use ini_set()",
|
2015-03-04 03:42:28 +08:00
|
|
|
PW32G(mail_host), !INI_INT("smtp_port") ? 25 : INI_INT("smtp_port"));
|
2002-05-15 04:40:39 +08:00
|
|
|
return FAILURE;
|
1999-04-08 05:05:13 +08:00
|
|
|
} else {
|
2017-02-01 20:43:33 +08:00
|
|
|
ret = SendText(RPath, Subject, mailTo, mailCc, mailBcc, data, headers ? ZSTR_VAL(headers_trim) : NULL, headers ? ZSTR_VAL(headers_lc) : NULL, error_message);
|
1999-04-08 05:05:13 +08:00
|
|
|
TSMClose();
|
2002-05-15 04:40:39 +08:00
|
|
|
if (RPath) {
|
|
|
|
efree(RPath);
|
|
|
|
}
|
2002-06-06 01:08:38 +08:00
|
|
|
if (headers) {
|
2019-03-29 18:12:09 +08:00
|
|
|
zend_string_release(headers_trim);
|
|
|
|
zend_string_release(headers_lc);
|
2002-05-18 00:16:27 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
if (ret != SUCCESS) {
|
|
|
|
*error = ret;
|
2002-05-15 04:40:39 +08:00
|
|
|
return FAILURE;
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
2002-05-15 04:40:39 +08:00
|
|
|
return SUCCESS;
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-10 19:59:06 +08:00
|
|
|
//*********************************************************************
|
1999-04-08 05:05:13 +08:00
|
|
|
// Name: TSendMail::~TSendMail
|
|
|
|
// Input:
|
|
|
|
// Output:
|
|
|
|
// Description: DESTRUCTOR
|
|
|
|
// Author/Date: jcar 20/9/96
|
|
|
|
// History:
|
2024-08-10 19:59:06 +08:00
|
|
|
//*********************************************************************
|
2020-07-23 00:59:00 +08:00
|
|
|
PHPAPI void TSMClose(void)
|
1999-04-08 05:05:13 +08:00
|
|
|
{
|
2000-07-10 22:49:20 +08:00
|
|
|
Post("QUIT\r\n");
|
2002-05-15 04:40:39 +08:00
|
|
|
Ack(NULL);
|
2015-01-03 17:22:58 +08:00
|
|
|
/* to guarantee that the cleanup is not made twice and
|
2021-03-31 11:49:41 +08:00
|
|
|
compromise the rest of the application if sockets are used
|
|
|
|
elsewhere
|
2000-06-17 02:24:02 +08:00
|
|
|
*/
|
2000-07-28 20:21:44 +08:00
|
|
|
|
2015-03-04 03:42:28 +08:00
|
|
|
shutdown(PW32G(mail_socket), 0);
|
|
|
|
closesocket(PW32G(mail_socket));
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-10 19:59:06 +08:00
|
|
|
//*********************************************************************
|
1999-04-08 05:05:13 +08:00
|
|
|
// Name: char *GetSMErrorText
|
2021-03-31 11:49:41 +08:00
|
|
|
// Input: Error index returned by the member functions
|
1999-04-08 05:05:13 +08:00
|
|
|
// Output: pointer to a string containing the error description
|
|
|
|
// Description:
|
|
|
|
// Author/Date: jcar 20/9/96
|
|
|
|
// History:
|
2024-08-10 19:59:06 +08:00
|
|
|
//*********************************************************************
|
2003-12-09 06:10:42 +08:00
|
|
|
PHPAPI char *GetSMErrorText(int index)
|
1999-04-08 05:05:13 +08:00
|
|
|
{
|
2002-05-14 21:52:18 +08:00
|
|
|
if (MIN_ERROR_INDEX <= index && index < MAX_ERROR_INDEX) {
|
1999-04-08 05:05:13 +08:00
|
|
|
return (ErrorMessages[index]);
|
2002-05-14 23:32:15 +08:00
|
|
|
|
2002-05-14 21:52:18 +08:00
|
|
|
} else {
|
|
|
|
return (ErrorMessages[UNKNOWN_ERROR]);
|
2002-05-14 23:32:15 +08:00
|
|
|
|
2002-05-14 21:52:18 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
|
|
|
|
2021-03-01 23:18:40 +08:00
|
|
|
/* strtok_r like, but recognizes quoted-strings */
|
|
|
|
static char *find_address(char *list, char **state)
|
|
|
|
{
|
2021-06-18 19:25:39 +08:00
|
|
|
bool in_quotes = 0;
|
2021-03-01 23:18:40 +08:00
|
|
|
char *p = list;
|
|
|
|
|
|
|
|
if (list == NULL) {
|
|
|
|
if (*state == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
p = list = *state;
|
|
|
|
}
|
|
|
|
*state = NULL;
|
|
|
|
while ((p = strpbrk(p, ",\"\\")) != NULL) {
|
|
|
|
if (*p == '\\' && in_quotes) {
|
|
|
|
if (p[1] == '\0') {
|
|
|
|
/* invalid address; let SMTP server deal with it */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
} else if (*p == '"') {
|
|
|
|
in_quotes = !in_quotes;
|
|
|
|
} else if (*p == ',' && !in_quotes) {
|
|
|
|
*p = '\0';
|
|
|
|
*state = p + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2024-08-10 19:59:06 +08:00
|
|
|
//*********************************************************************
|
2006-01-06 10:04:33 +08:00
|
|
|
// Name: SendText
|
1999-04-08 05:05:13 +08:00
|
|
|
// 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.
|
2002-05-18 00:16:27 +08:00
|
|
|
// 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
|
1999-04-08 05:05:13 +08:00
|
|
|
// Output: Error code or SUCCESS
|
|
|
|
// Description:
|
|
|
|
// Author/Date: jcar 20/9/96
|
|
|
|
// History:
|
2024-08-10 19:59:06 +08:00
|
|
|
//*********************************************************************
|
2020-06-07 17:01:19 +08:00
|
|
|
static int SendText(char *RPath, const char *Subject, const char *mailTo, char *mailCc, char *mailBcc, const char *data,
|
|
|
|
const char *headers, char *headers_lc, char **error_message)
|
1999-04-08 05:05:13 +08:00
|
|
|
{
|
2003-09-09 06:36:59 +08:00
|
|
|
int res;
|
1999-04-08 05:05:13 +08:00
|
|
|
char *p;
|
2021-03-02 01:50:11 +08:00
|
|
|
char *tempMailTo, *token, *token_state;
|
2020-06-07 17:01:19 +08:00
|
|
|
const char *pos1, *pos2;
|
2002-05-15 04:40:39 +08:00
|
|
|
char *server_response = NULL;
|
2002-05-15 18:11:33 +08:00
|
|
|
char *stripped_header = NULL;
|
2014-05-13 12:51:11 +08:00
|
|
|
zend_string *data_cln;
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2000-06-17 02:24:02 +08:00
|
|
|
/* check for NULL parameters */
|
1999-04-08 05:05:13 +08:00
|
|
|
if (data == NULL)
|
|
|
|
return (BAD_MSG_CONTENTS);
|
|
|
|
if (mailTo == NULL)
|
|
|
|
return (BAD_MSG_DESTINATION);
|
|
|
|
if (RPath == NULL)
|
|
|
|
return (BAD_MSG_RPATH);
|
|
|
|
|
2000-06-17 02:24:02 +08:00
|
|
|
/* simple checks for the mailto address */
|
|
|
|
/* have ampersand ? */
|
2002-05-15 04:40:39 +08:00
|
|
|
/* 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.
|
1999-04-08 05:05:13 +08:00
|
|
|
if (strchr(mailTo, '@') == NULL)
|
|
|
|
return (BAD_MSG_DESTINATION);
|
2002-05-15 04:40:39 +08:00
|
|
|
*/
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2015-03-04 03:42:28 +08:00
|
|
|
snprintf(PW32G(mail_buffer), sizeof(PW32G(mail_buffer)), "HELO %s\r\n", PW32G(mail_local_host));
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2013-07-13 20:37:04 +08:00
|
|
|
/* in the beginning of the dialog */
|
2000-06-17 02:24:02 +08:00
|
|
|
/* attempt reconnect if the first Post fail */
|
2015-03-04 03:42:28 +08:00
|
|
|
if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
|
2015-03-03 22:37:08 +08:00
|
|
|
int err = MailConnect();
|
|
|
|
if (0 != err) {
|
|
|
|
return (FAILED_TO_SEND);
|
|
|
|
}
|
|
|
|
|
2015-03-04 03:42:28 +08:00
|
|
|
if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
|
1999-04-08 05:05:13 +08:00
|
|
|
return (res);
|
2006-01-06 10:04:33 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
2002-05-15 04:40:39 +08:00
|
|
|
if ((res = Ack(&server_response)) != SUCCESS) {
|
|
|
|
SMTP_ERROR_RESPONSE(server_response);
|
1999-04-08 05:05:13 +08:00
|
|
|
return (res);
|
2002-05-15 04:40:39 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2006-01-06 10:04:33 +08:00
|
|
|
SMTP_SKIP_SPACE(RPath);
|
2015-03-04 03:42:28 +08:00
|
|
|
FormatEmailAddress(PW32G(mail_buffer), RPath, "MAIL FROM:<%s>\r\n");
|
|
|
|
if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
|
2000-06-28 02:41:44 +08:00
|
|
|
return (res);
|
2006-01-06 10:04:33 +08:00
|
|
|
}
|
2002-05-15 04:40:39 +08:00
|
|
|
if ((res = Ack(&server_response)) != SUCCESS) {
|
|
|
|
SMTP_ERROR_RESPONSE(server_response);
|
2002-05-14 21:52:18 +08:00
|
|
|
return W32_SM_SENDMAIL_FROM_MALFORMED;
|
2002-05-15 04:40:39 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2000-07-10 22:49:20 +08:00
|
|
|
tempMailTo = estrdup(mailTo);
|
|
|
|
/* Send mail to all rcpt's */
|
2021-03-01 23:18:40 +08:00
|
|
|
token = find_address(tempMailTo, &token_state);
|
2006-01-06 10:04:33 +08:00
|
|
|
while (token != NULL)
|
2000-07-10 22:49:20 +08:00
|
|
|
{
|
2006-01-06 10:04:33 +08:00
|
|
|
SMTP_SKIP_SPACE(token);
|
2015-03-04 03:42:28 +08:00
|
|
|
FormatEmailAddress(PW32G(mail_buffer), token, "RCPT TO:<%s>\r\n");
|
|
|
|
if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
|
2002-06-06 01:08:38 +08:00
|
|
|
efree(tempMailTo);
|
2000-07-10 22:49:20 +08:00
|
|
|
return (res);
|
2002-06-06 01:08:38 +08:00
|
|
|
}
|
2002-05-15 04:40:39 +08:00
|
|
|
if ((res = Ack(&server_response)) != SUCCESS) {
|
|
|
|
SMTP_ERROR_RESPONSE(server_response);
|
2002-06-06 01:08:38 +08:00
|
|
|
efree(tempMailTo);
|
2000-07-10 22:49:20 +08:00
|
|
|
return (res);
|
2002-05-15 04:40:39 +08:00
|
|
|
}
|
2021-03-01 23:18:40 +08:00
|
|
|
token = find_address(NULL, &token_state);
|
2000-07-10 22:49:20 +08:00
|
|
|
}
|
2002-05-15 18:11:33 +08:00
|
|
|
efree(tempMailTo);
|
2000-07-10 22:49:20 +08:00
|
|
|
|
2002-12-01 05:50:17 +08:00
|
|
|
if (mailCc && *mailCc) {
|
|
|
|
tempMailTo = estrdup(mailCc);
|
|
|
|
/* Send mail to all rcpt's */
|
2021-03-01 23:18:40 +08:00
|
|
|
token = find_address(tempMailTo, &token_state);
|
2006-01-06 10:04:33 +08:00
|
|
|
while (token != NULL)
|
2002-12-01 05:50:17 +08:00
|
|
|
{
|
2006-01-06 10:04:33 +08:00
|
|
|
SMTP_SKIP_SPACE(token);
|
2015-03-04 03:42:28 +08:00
|
|
|
FormatEmailAddress(PW32G(mail_buffer), token, "RCPT TO:<%s>\r\n");
|
|
|
|
if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
|
2002-12-01 05:50:17 +08:00
|
|
|
efree(tempMailTo);
|
|
|
|
return (res);
|
|
|
|
}
|
|
|
|
if ((res = Ack(&server_response)) != SUCCESS) {
|
|
|
|
SMTP_ERROR_RESPONSE(server_response);
|
|
|
|
efree(tempMailTo);
|
|
|
|
return (res);
|
|
|
|
}
|
2021-03-01 23:18:40 +08:00
|
|
|
token = find_address(NULL, &token_state);
|
2002-12-01 05:50:17 +08:00
|
|
|
}
|
|
|
|
efree(tempMailTo);
|
|
|
|
}
|
2000-07-10 22:49:20 +08:00
|
|
|
/* Send mail to all Cc rcpt's */
|
2005-01-20 02:54:23 +08:00
|
|
|
else if (headers && (pos1 = strstr(headers_lc, "cc:")) && ((pos1 == headers_lc) || (*(pos1-1) == '\n'))) {
|
2002-05-18 00:16:27 +08:00
|
|
|
/* Real offset is memaddress from the original headers + difference of
|
2021-03-31 11:49:41 +08:00
|
|
|
* string found in the lowercase headers + 3 characters to jump over
|
2002-05-18 00:16:27 +08:00
|
|
|
* the cc: */
|
|
|
|
pos1 = headers + (pos1 - headers_lc) + 3;
|
2002-05-14 21:52:18 +08:00
|
|
|
if (NULL == (pos2 = strstr(pos1, "\r\n"))) {
|
|
|
|
tempMailTo = estrndup(pos1, strlen(pos1));
|
|
|
|
} else {
|
2020-11-10 23:13:54 +08:00
|
|
|
char *pos3;
|
|
|
|
while (pos2[2] == ' ' || pos2[2] == '\t') {
|
|
|
|
pos3 = strstr(pos2 + 2, "\r\n");
|
|
|
|
if (pos3 != NULL) {
|
|
|
|
pos2 = pos3;
|
|
|
|
} else {
|
|
|
|
pos2 += strlen(pos2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-01-06 10:04:33 +08:00
|
|
|
tempMailTo = estrndup(pos1, pos2 - pos1);
|
2002-05-14 21:52:18 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2021-03-01 23:18:40 +08:00
|
|
|
token = find_address(tempMailTo, &token_state);
|
2006-01-06 10:04:33 +08:00
|
|
|
while (token != NULL)
|
2000-07-10 22:49:20 +08:00
|
|
|
{
|
2003-07-24 00:03:10 +08:00
|
|
|
SMTP_SKIP_SPACE(token);
|
2015-03-04 03:42:28 +08:00
|
|
|
FormatEmailAddress(PW32G(mail_buffer), token, "RCPT TO:<%s>\r\n");
|
|
|
|
if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
|
2006-01-06 10:04:33 +08:00
|
|
|
efree(tempMailTo);
|
2000-07-10 22:49:20 +08:00
|
|
|
return (res);
|
2006-01-06 10:04:33 +08:00
|
|
|
}
|
2002-05-15 04:40:39 +08:00
|
|
|
if ((res = Ack(&server_response)) != SUCCESS) {
|
|
|
|
SMTP_ERROR_RESPONSE(server_response);
|
2006-01-06 10:04:33 +08:00
|
|
|
efree(tempMailTo);
|
2002-07-03 06:29:13 +08:00
|
|
|
return (res);
|
|
|
|
}
|
2021-03-01 23:18:40 +08:00
|
|
|
token = find_address(NULL,&token_state);
|
2002-07-03 06:29:13 +08:00
|
|
|
}
|
|
|
|
efree(tempMailTo);
|
|
|
|
}
|
|
|
|
|
2002-12-01 05:50:17 +08:00
|
|
|
/* 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);
|
2002-07-03 06:29:13 +08:00
|
|
|
/* Send mail to all rcpt's */
|
2021-03-01 23:18:40 +08:00
|
|
|
token = find_address(tempMailTo, &token_state);
|
2006-01-06 10:04:33 +08:00
|
|
|
while (token != NULL)
|
2002-07-03 06:29:13 +08:00
|
|
|
{
|
2003-07-24 00:03:10 +08:00
|
|
|
SMTP_SKIP_SPACE(token);
|
2015-03-04 03:42:28 +08:00
|
|
|
FormatEmailAddress(PW32G(mail_buffer), token, "RCPT TO:<%s>\r\n");
|
|
|
|
if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
|
2002-07-03 06:29:13 +08:00
|
|
|
efree(tempMailTo);
|
|
|
|
return (res);
|
|
|
|
}
|
|
|
|
if ((res = Ack(&server_response)) != SUCCESS) {
|
|
|
|
SMTP_ERROR_RESPONSE(server_response);
|
|
|
|
efree(tempMailTo);
|
2000-07-10 22:49:20 +08:00
|
|
|
return (res);
|
2002-05-15 04:40:39 +08:00
|
|
|
}
|
2021-03-01 23:18:40 +08:00
|
|
|
token = find_address(NULL, &token_state);
|
2000-07-10 22:49:20 +08:00
|
|
|
}
|
|
|
|
efree(tempMailTo);
|
|
|
|
}
|
2002-12-01 05:50:17 +08:00
|
|
|
else if (headers) {
|
2017-05-02 18:31:00 +08:00
|
|
|
if ((pos1 = strstr(headers_lc, "bcc:")) && (pos1 == headers_lc || *(pos1-1) == '\n')) {
|
2002-05-18 00:16:27 +08:00
|
|
|
/* Real offset is memaddress from the original headers + difference of
|
2021-03-31 11:49:41 +08:00
|
|
|
* string found in the lowercase headers + 4 characters to jump over
|
2002-05-18 00:16:27 +08:00
|
|
|
* 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 {
|
2021-02-09 06:11:37 +08:00
|
|
|
const char *pos3 = pos2;
|
2020-11-10 23:13:54 +08:00
|
|
|
while (pos2[2] == ' ' || pos2[2] == '\t') {
|
|
|
|
pos3 = strstr(pos2 + 2, "\r\n");
|
|
|
|
if (pos3 != NULL) {
|
|
|
|
pos2 = pos3;
|
|
|
|
} else {
|
|
|
|
pos2 += strlen(pos2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2002-05-18 00:16:27 +08:00
|
|
|
tempMailTo = estrndup(pos1, pos2 - pos1);
|
2020-11-10 23:13:54 +08:00
|
|
|
if (pos3 == NULL) {
|
|
|
|
/* Later, when we remove the Bcc: out of the
|
|
|
|
header we know it was the last thing. */
|
|
|
|
pos2 = pos1;
|
|
|
|
}
|
2002-05-15 18:11:33 +08:00
|
|
|
}
|
2002-05-18 00:16:27 +08:00
|
|
|
|
2021-03-01 23:18:40 +08:00
|
|
|
token = find_address(tempMailTo, &token_state);
|
2006-01-06 10:04:33 +08:00
|
|
|
while (token != NULL)
|
2002-05-18 00:16:27 +08:00
|
|
|
{
|
2003-07-24 00:03:10 +08:00
|
|
|
SMTP_SKIP_SPACE(token);
|
2015-03-04 03:42:28 +08:00
|
|
|
FormatEmailAddress(PW32G(mail_buffer), token, "RCPT TO:<%s>\r\n");
|
|
|
|
if ((res = Post(PW32G(mail_buffer))) != SUCCESS) {
|
2006-01-06 10:04:33 +08:00
|
|
|
efree(tempMailTo);
|
2002-05-18 00:16:27 +08:00
|
|
|
return (res);
|
|
|
|
}
|
|
|
|
if ((res = Ack(&server_response)) != SUCCESS) {
|
|
|
|
SMTP_ERROR_RESPONSE(server_response);
|
2006-01-06 10:04:33 +08:00
|
|
|
efree(tempMailTo);
|
2002-05-18 00:16:27 +08:00
|
|
|
return (res);
|
|
|
|
}
|
2021-03-01 23:18:40 +08:00
|
|
|
token = find_address(NULL, &token_state);
|
2002-05-15 18:11:33 +08:00
|
|
|
}
|
2002-05-18 00:16:27 +08:00
|
|
|
efree(tempMailTo);
|
2002-05-15 18:11:33 +08:00
|
|
|
|
2002-05-18 00:16:27 +08:00
|
|
|
/* Now that we've identified that we've a Bcc list,
|
|
|
|
remove it from the current header. */
|
2017-03-13 00:19:20 +08:00
|
|
|
stripped_header = ecalloc(1, strlen(headers));
|
2002-05-18 00:16:27 +08:00
|
|
|
/* 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
|
2014-11-20 03:59:31 +08:00
|
|
|
we've to subtract them here. */
|
2002-05-18 00:16:27 +08:00
|
|
|
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);
|
|
|
|
}
|
2002-12-07 06:56:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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) {
|
2017-03-13 00:19:20 +08:00
|
|
|
stripped_header = estrndup(headers, strlen(headers));
|
2002-05-15 18:11:33 +08:00
|
|
|
}
|
2002-07-03 07:44:06 +08:00
|
|
|
|
2002-05-15 18:11:33 +08:00
|
|
|
if ((res = Post("DATA\r\n")) != SUCCESS) {
|
2003-01-07 19:24:17 +08:00
|
|
|
if (stripped_header) {
|
|
|
|
efree(stripped_header);
|
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
return (res);
|
2002-05-15 18:11:33 +08:00
|
|
|
}
|
2002-05-15 04:40:39 +08:00
|
|
|
if ((res = Ack(&server_response)) != SUCCESS) {
|
|
|
|
SMTP_ERROR_RESPONSE(server_response);
|
2003-01-07 19:24:17 +08:00
|
|
|
if (stripped_header) {
|
|
|
|
efree(stripped_header);
|
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
return (res);
|
2002-05-15 04:40:39 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2000-06-17 02:24:02 +08:00
|
|
|
/* send message header */
|
2002-05-15 18:11:33 +08:00
|
|
|
if (Subject == NULL) {
|
2014-12-14 06:06:14 +08:00
|
|
|
res = PostHeader(RPath, "No Subject", mailTo, stripped_header);
|
2002-05-15 18:11:33 +08:00
|
|
|
} else {
|
2014-12-14 06:06:14 +08:00
|
|
|
res = PostHeader(RPath, Subject, mailTo, stripped_header);
|
2002-05-15 18:11:33 +08:00
|
|
|
}
|
2002-05-18 00:16:27 +08:00
|
|
|
if (stripped_header) {
|
|
|
|
efree(stripped_header);
|
|
|
|
}
|
2002-05-15 18:11:33 +08:00
|
|
|
if (res != SUCCESS) {
|
1999-04-08 05:05:13 +08:00
|
|
|
return (res);
|
2002-05-15 18:11:33 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2003-07-24 00:03:10 +08:00
|
|
|
/* 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,
|
2014-05-13 12:51:11 +08:00
|
|
|
PHP_WIN32_MAIL_DOT_REPLACE, sizeof(PHP_WIN32_MAIL_DOT_REPLACE) - 1);
|
2003-09-09 06:36:59 +08:00
|
|
|
if (!data_cln) {
|
2015-06-29 21:44:54 +08:00
|
|
|
data_cln = ZSTR_EMPTY_ALLOC();
|
2003-09-09 06:36:59 +08:00
|
|
|
}
|
2003-07-24 00:03:10 +08:00
|
|
|
|
2000-06-17 02:24:02 +08:00
|
|
|
/* send message contents in 1024 chunks */
|
2003-09-09 06:36:59 +08:00
|
|
|
{
|
2017-02-01 19:45:19 +08:00
|
|
|
char c, *e2, *e = ZSTR_VAL(data_cln) + ZSTR_LEN(data_cln);
|
|
|
|
p = ZSTR_VAL(data_cln);
|
2003-07-24 00:03:10 +08:00
|
|
|
|
2003-09-09 06:36:59 +08:00
|
|
|
while (e - p > 1024) {
|
|
|
|
e2 = p + 1024;
|
|
|
|
c = *e2;
|
|
|
|
*e2 = '\0';
|
|
|
|
if ((res = Post(p)) != SUCCESS) {
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_string_free(data_cln);
|
2003-09-09 06:36:59 +08:00
|
|
|
return(res);
|
2003-07-24 00:03:10 +08:00
|
|
|
}
|
2003-09-09 06:36:59 +08:00
|
|
|
*e2 = c;
|
|
|
|
p = e2;
|
2003-07-24 00:03:10 +08:00
|
|
|
}
|
2003-09-09 06:36:59 +08:00
|
|
|
if ((res = Post(p)) != SUCCESS) {
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_string_free(data_cln);
|
2003-09-09 06:36:59 +08:00
|
|
|
return(res);
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_string_free(data_cln);
|
2003-07-24 00:03:10 +08:00
|
|
|
|
2000-06-17 02:24:02 +08:00
|
|
|
/*send termination dot */
|
1999-04-08 05:05:13 +08:00
|
|
|
if ((res = Post("\r\n.\r\n")) != SUCCESS)
|
|
|
|
return (res);
|
2002-05-15 04:40:39 +08:00
|
|
|
if ((res = Ack(&server_response)) != SUCCESS) {
|
|
|
|
SMTP_ERROR_RESPONSE(server_response);
|
1999-04-08 05:05:13 +08:00
|
|
|
return (res);
|
2002-05-15 04:40:39 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
return (SUCCESS);
|
|
|
|
}
|
|
|
|
|
2020-06-07 17:01:19 +08:00
|
|
|
static int addToHeader(char **header_buffer, const char *specifier, const char *string)
|
2005-08-08 06:06:29 +08:00
|
|
|
{
|
2024-06-10 01:40:12 +08:00
|
|
|
size_t header_buffer_size = strlen(*header_buffer);
|
|
|
|
size_t total_size = header_buffer_size + strlen(specifier) + strlen(string) + 1;
|
|
|
|
*header_buffer = erealloc(*header_buffer, total_size);
|
|
|
|
snprintf(*header_buffer + header_buffer_size, total_size - header_buffer_size, specifier, string);
|
2002-06-03 06:10:25 +08:00
|
|
|
return 1;
|
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2024-08-10 19:59:06 +08:00
|
|
|
//*********************************************************************
|
1999-04-08 05:05:13 +08:00
|
|
|
// Name: PostHeader
|
|
|
|
// Input: 1) return path
|
2001-12-05 07:33:52 +08:00
|
|
|
// 2) Subject
|
|
|
|
// 3) destination address
|
|
|
|
// 4) headers
|
1999-04-08 05:05:13 +08:00
|
|
|
// Output: Error code or Success
|
|
|
|
// Description:
|
|
|
|
// Author/Date: jcar 20/9/96
|
|
|
|
// History:
|
2024-08-10 19:59:06 +08:00
|
|
|
//*********************************************************************
|
2020-06-07 17:01:19 +08:00
|
|
|
static int PostHeader(char *RPath, const char *Subject, const char *mailTo, char *xheaders)
|
1999-04-08 05:05:13 +08:00
|
|
|
{
|
2000-06-17 02:24:02 +08:00
|
|
|
/* Print message header according to RFC 822 */
|
|
|
|
/* Return-path, Received, Date, From, Subject, Sender, To, cc */
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2005-08-08 06:06:29 +08:00
|
|
|
int res;
|
2002-06-03 06:10:25 +08:00
|
|
|
char *header_buffer;
|
2002-05-18 00:16:27 +08:00
|
|
|
char *headers_lc = NULL;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (xheaders) {
|
2017-07-25 13:47:58 +08:00
|
|
|
size_t headers_lc_len;
|
|
|
|
|
2017-03-13 00:19:20 +08:00
|
|
|
headers_lc = estrdup(xheaders);
|
2017-07-25 13:47:58 +08:00
|
|
|
headers_lc_len = strlen(headers_lc);
|
|
|
|
|
|
|
|
for (i = 0; i < headers_lc_len; i++) {
|
2002-05-18 00:16:27 +08:00
|
|
|
headers_lc[i] = tolower(headers_lc[i]);
|
|
|
|
}
|
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2007-02-24 10:17:47 +08:00
|
|
|
header_buffer = ecalloc(1, MAIL_BUFFER_SIZE);
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2005-08-08 06:06:29 +08:00
|
|
|
if (!xheaders || !strstr(headers_lc, "date:")) {
|
|
|
|
time_t tNow = time(NULL);
|
2014-12-14 06:06:14 +08:00
|
|
|
zend_string *dt = php_format_date("r", 1, tNow, 1);
|
2005-08-08 06:06:29 +08:00
|
|
|
|
2017-02-01 19:45:19 +08:00
|
|
|
snprintf(header_buffer, MAIL_BUFFER_SIZE, "Date: %s\r\n", ZSTR_VAL(dt));
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_string_free(dt);
|
1999-06-10 17:10:07 +08:00
|
|
|
}
|
|
|
|
|
2002-06-03 06:10:25 +08:00
|
|
|
if (!headers_lc || !strstr(headers_lc, "from:")) {
|
|
|
|
if (!addToHeader(&header_buffer, "From: %s\r\n", RPath)) {
|
|
|
|
goto PostHeader_outofmem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!addToHeader(&header_buffer, "Subject: %s\r\n", Subject)) {
|
|
|
|
goto PostHeader_outofmem;
|
|
|
|
}
|
2002-06-03 07:39:59 +08:00
|
|
|
|
|
|
|
/* Only add the To: field from the $to parameter if isn't in the custom headers */
|
|
|
|
if ((headers_lc && (!strstr(headers_lc, "\r\nto:") && (strncmp(headers_lc, "to:", 3) != 0))) || !headers_lc) {
|
|
|
|
if (!addToHeader(&header_buffer, "To: %s\r\n", mailTo)) {
|
|
|
|
goto PostHeader_outofmem;
|
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
2006-01-06 10:04:33 +08:00
|
|
|
if (xheaders) {
|
2002-07-03 07:44:06 +08:00
|
|
|
if (!addToHeader(&header_buffer, "%s\r\n", xheaders)) {
|
2002-06-03 06:10:25 +08:00
|
|
|
goto PostHeader_outofmem;
|
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
|
|
|
|
2002-06-03 22:39:30 +08:00
|
|
|
if (headers_lc) {
|
|
|
|
efree(headers_lc);
|
|
|
|
}
|
2002-06-03 06:10:25 +08:00
|
|
|
if ((res = Post(header_buffer)) != SUCCESS) {
|
|
|
|
efree(header_buffer);
|
1999-04-08 05:05:13 +08:00
|
|
|
return (res);
|
2002-05-18 00:16:27 +08:00
|
|
|
}
|
2002-06-03 06:10:25 +08:00
|
|
|
efree(header_buffer);
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2002-05-18 00:16:27 +08:00
|
|
|
if ((res = Post("\r\n")) != SUCCESS) {
|
1999-04-08 05:05:13 +08:00
|
|
|
return (res);
|
2002-05-18 00:16:27 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
return (SUCCESS);
|
2002-06-03 06:10:25 +08:00
|
|
|
|
|
|
|
PostHeader_outofmem:
|
2002-06-03 22:39:30 +08:00
|
|
|
if (headers_lc) {
|
|
|
|
efree(headers_lc);
|
|
|
|
}
|
2002-06-03 06:10:25 +08:00
|
|
|
return OUT_OF_MEMORY;
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-08-10 19:59:06 +08:00
|
|
|
//*********************************************************************
|
1999-04-08 05:05:13 +08:00
|
|
|
// Name: MailConnect
|
|
|
|
// Input: None
|
|
|
|
// Output: None
|
|
|
|
// Description: Connect to the mail host and receive the welcome message.
|
|
|
|
// Author/Date: jcar 20/9/96
|
|
|
|
// History:
|
2024-08-10 19:59:06 +08:00
|
|
|
//*********************************************************************
|
2005-08-08 06:06:29 +08:00
|
|
|
static int MailConnect()
|
1999-04-08 05:05:13 +08:00
|
|
|
{
|
|
|
|
|
2009-06-30 19:39:15 +08:00
|
|
|
int res, namelen;
|
2000-08-07 05:42:10 +08:00
|
|
|
short portnum;
|
2009-06-30 19:39:15 +08:00
|
|
|
struct hostent *ent;
|
|
|
|
IN_ADDR addr;
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
IN6_ADDR addr6;
|
|
|
|
#endif
|
2015-03-04 03:42:28 +08:00
|
|
|
SOCKADDR_IN sock_in;
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2017-02-01 01:25:36 +08:00
|
|
|
#if SENDMAIL_DEBUG
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
2000-06-17 02:24:02 +08:00
|
|
|
/* Create Socket */
|
2015-03-04 03:42:28 +08:00
|
|
|
if ((PW32G(mail_socket) = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
|
1999-04-08 05:05:13 +08:00
|
|
|
return (FAILED_TO_OBTAIN_SOCKET_HANDLE);
|
2009-06-30 19:39:15 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2000-06-17 02:24:02 +08:00
|
|
|
/* Get our own host name */
|
2015-03-04 03:42:28 +08:00
|
|
|
if (gethostname(PW32G(mail_local_host), HOST_NAME_LEN)) {
|
|
|
|
closesocket(PW32G(mail_socket));
|
2009-06-30 19:39:15 +08:00
|
|
|
return (FAILED_TO_GET_HOSTNAME);
|
|
|
|
}
|
|
|
|
|
2015-03-04 03:42:28 +08:00
|
|
|
ent = gethostbyname(PW32G(mail_local_host));
|
2009-06-30 19:39:15 +08:00
|
|
|
|
|
|
|
if (!ent) {
|
2015-03-04 03:42:28 +08:00
|
|
|
closesocket(PW32G(mail_socket));
|
1999-04-08 05:05:13 +08:00
|
|
|
return (FAILED_TO_GET_HOSTNAME);
|
2009-06-30 19:39:15 +08:00
|
|
|
}
|
|
|
|
|
2014-10-29 02:25:55 +08:00
|
|
|
namelen = (int)strlen(ent->h_name);
|
2009-06-30 19:39:15 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
if (inet_pton(AF_INET, ent->h_name, &addr) == 1 || inet_pton(AF_INET6, ent->h_name, &addr6) == 1)
|
|
|
|
#else
|
|
|
|
if (inet_pton(AF_INET, ent->h_name, &addr) == 1)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if (namelen + 2 >= HOST_NAME_LEN) {
|
2015-03-04 03:42:28 +08:00
|
|
|
closesocket(PW32G(mail_socket));
|
2009-06-30 19:39:15 +08:00
|
|
|
return (FAILED_TO_GET_HOSTNAME);
|
|
|
|
}
|
|
|
|
|
2015-03-04 03:42:28 +08:00
|
|
|
strcpy(PW32G(mail_local_host), "[");
|
|
|
|
strcpy(PW32G(mail_local_host) + 1, ent->h_name);
|
|
|
|
strcpy(PW32G(mail_local_host) + namelen + 1, "]");
|
2009-06-30 19:39:15 +08:00
|
|
|
} else {
|
|
|
|
if (namelen >= HOST_NAME_LEN) {
|
2015-03-04 03:42:28 +08:00
|
|
|
closesocket(PW32G(mail_socket));
|
2009-06-30 19:39:15 +08:00
|
|
|
return (FAILED_TO_GET_HOSTNAME);
|
|
|
|
}
|
|
|
|
|
2015-03-04 03:42:28 +08:00
|
|
|
strcpy(PW32G(mail_local_host), ent->h_name);
|
2009-06-30 19:39:15 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2000-06-17 02:24:02 +08:00
|
|
|
/* Resolve the servers IP */
|
|
|
|
/*
|
2015-03-04 03:42:28 +08:00
|
|
|
if (!isdigit(PW32G(mail_host)[0])||!gethostbyname(PW32G(mail_host)))
|
2000-06-17 02:24:02 +08:00
|
|
|
{
|
|
|
|
return (FAILED_TO_RESOLVE_HOST);
|
|
|
|
}
|
2006-01-06 10:04:33 +08:00
|
|
|
*/
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2002-06-03 01:13:48 +08:00
|
|
|
portnum = (short) INI_INT("smtp_port");
|
2000-08-07 05:38:41 +08:00
|
|
|
if (!portnum) {
|
2000-08-07 01:49:41 +08:00
|
|
|
portnum = 25;
|
2000-08-07 05:38:41 +08:00
|
|
|
}
|
2000-08-07 01:49:41 +08:00
|
|
|
|
2000-06-17 02:24:02 +08:00
|
|
|
/* Connect to server */
|
1999-04-24 08:12:00 +08:00
|
|
|
sock_in.sin_family = AF_INET;
|
2000-08-07 01:49:41 +08:00
|
|
|
sock_in.sin_port = htons(portnum);
|
2015-03-04 03:42:28 +08:00
|
|
|
sock_in.sin_addr.S_un.S_addr = GetAddr(PW32G(mail_host));
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2015-03-04 03:42:28 +08:00
|
|
|
if (connect(PW32G(mail_socket), (LPSOCKADDR) & sock_in, sizeof(sock_in))) {
|
|
|
|
closesocket(PW32G(mail_socket));
|
1999-04-08 05:05:13 +08:00
|
|
|
return (FAILED_TO_CONNECT);
|
2009-06-30 19:39:15 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2000-06-17 02:24:02 +08:00
|
|
|
/* receive Server welcome message */
|
2002-05-15 04:40:39 +08:00
|
|
|
res = Ack(NULL);
|
1999-04-08 05:05:13 +08:00
|
|
|
return (res);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-10 19:59:06 +08:00
|
|
|
//*********************************************************************
|
1999-04-08 05:05:13 +08:00
|
|
|
// Name: Post
|
|
|
|
// Input:
|
|
|
|
// Output:
|
|
|
|
// Description:
|
|
|
|
// Author/Date: jcar 20/9/96
|
|
|
|
// History:
|
2024-08-10 19:59:06 +08:00
|
|
|
//*********************************************************************
|
2005-08-08 06:06:29 +08:00
|
|
|
static int Post(LPCSTR msg)
|
1999-04-08 05:05:13 +08:00
|
|
|
{
|
2014-10-29 02:25:55 +08:00
|
|
|
int len = (int)strlen(msg);
|
1999-04-08 05:05:13 +08:00
|
|
|
int slen;
|
|
|
|
int index = 0;
|
|
|
|
|
2017-02-01 01:25:36 +08:00
|
|
|
#if SENDMAIL_DEBUG
|
|
|
|
if (msg)
|
|
|
|
printf("POST: '%s'\n", msg);
|
|
|
|
return (SUCCESS);
|
|
|
|
#endif
|
|
|
|
|
1999-04-08 05:05:13 +08:00
|
|
|
while (len > 0) {
|
2015-03-04 03:42:28 +08:00
|
|
|
if ((slen = send(PW32G(mail_socket), msg + index, len, 0)) < 1)
|
1999-04-08 05:05:13 +08:00
|
|
|
return (FAILED_TO_SEND);
|
|
|
|
len -= slen;
|
|
|
|
index += slen;
|
|
|
|
}
|
|
|
|
return (SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-08-10 19:59:06 +08:00
|
|
|
//*********************************************************************
|
1999-04-08 05:05:13 +08:00
|
|
|
// Name: Ack
|
|
|
|
// Input:
|
|
|
|
// Output:
|
|
|
|
// Description:
|
|
|
|
// Get the response from the server. We only want to know if the
|
|
|
|
// last command was successful.
|
|
|
|
// Author/Date: jcar 20/9/96
|
|
|
|
// History:
|
2024-08-10 19:59:06 +08:00
|
|
|
//*********************************************************************
|
2005-08-08 06:06:29 +08:00
|
|
|
static int Ack(char **server_response)
|
1999-04-08 05:05:13 +08:00
|
|
|
{
|
2015-07-29 00:38:06 +08:00
|
|
|
ZEND_TLS char buf[MAIL_BUFFER_SIZE];
|
1999-04-08 05:05:13 +08:00
|
|
|
int rlen;
|
|
|
|
int Index = 0;
|
|
|
|
int Received = 0;
|
|
|
|
|
2017-02-01 01:25:36 +08:00
|
|
|
#if SENDMAIL_DEBUG
|
|
|
|
return (SUCCESS);
|
|
|
|
#endif
|
|
|
|
|
2006-01-06 10:04:33 +08:00
|
|
|
again:
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2015-03-04 03:42:28 +08:00
|
|
|
if ((rlen = recv(PW32G(mail_socket), buf + Index, ((MAIL_BUFFER_SIZE) - 1) - Received, 0)) < 1) {
|
1999-04-08 05:05:13 +08:00
|
|
|
return (FAILED_TO_RECEIVE);
|
2006-01-06 10:04:33 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
Received += rlen;
|
|
|
|
buf[Received] = 0;
|
2000-06-17 02:24:02 +08:00
|
|
|
/*err_msg fprintf(stderr,"Received: (%d bytes) %s", rlen, buf + Index); */
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2000-06-17 02:24:02 +08:00
|
|
|
/* Check for newline */
|
1999-04-08 05:05:13 +08:00
|
|
|
Index += rlen;
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2020-01-16 19:04:00 +08:00
|
|
|
/* SMTP RFC says \r\n is the only valid line ending, who are we to argue ;)
|
2003-08-12 02:03:24 +08:00
|
|
|
* The response code must contain at least 5 characters ex. 220\r\n */
|
|
|
|
if (Received < 5 || buf[Received - 1] != '\n' || buf[Received - 2] != '\r') {
|
|
|
|
goto again;
|
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2002-05-15 04:40:39 +08:00
|
|
|
if (buf[0] > '3') {
|
2002-06-03 00:25:06 +08:00
|
|
|
/* If we've a valid pointer, return the SMTP server response so the error message contains more information */
|
2002-05-15 04:40:39 +08:00
|
|
|
if (server_response) {
|
|
|
|
int dec = 0;
|
|
|
|
/* See if we have something like \r, \n, \r\n or \n\r at the end of the message and chop it off */
|
|
|
|
if (Received > 2) {
|
|
|
|
if (buf[Received-1] == '\n' || buf[Received-1] == '\r') {
|
|
|
|
dec++;
|
|
|
|
if (buf[Received-2] == '\r' || buf[Received-2] == '\n') {
|
|
|
|
dec++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
*server_response = estrndup(buf, Received - dec);
|
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
return (SMTP_SERVER_ERROR);
|
2002-05-15 04:40:39 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
return (SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-10 19:59:06 +08:00
|
|
|
//*********************************************************************
|
1999-04-08 05:05:13 +08:00
|
|
|
// Name: unsigned long GetAddr (LPSTR szHost)
|
|
|
|
// Input:
|
|
|
|
// Output:
|
|
|
|
// Description: Given a string, it will return an IP address.
|
|
|
|
// - first it tries to convert the string directly
|
|
|
|
// - if that fails, it tries o resolve it as a hostname
|
|
|
|
//
|
|
|
|
// WARNING: gethostbyname() is a blocking function
|
|
|
|
// Author/Date: jcar 20/9/96
|
|
|
|
// History:
|
2024-08-10 19:59:06 +08:00
|
|
|
//*********************************************************************
|
2005-08-08 06:06:29 +08:00
|
|
|
static unsigned long GetAddr(LPSTR szHost)
|
1999-04-08 05:05:13 +08:00
|
|
|
{
|
|
|
|
LPHOSTENT lpstHost;
|
|
|
|
u_long lAddr = INADDR_ANY;
|
|
|
|
|
|
|
|
/* check that we have a string */
|
|
|
|
if (*szHost) {
|
|
|
|
|
|
|
|
/* check for a dotted-IP address string */
|
|
|
|
lAddr = inet_addr(szHost);
|
|
|
|
|
|
|
|
/* If not an address, then try to resolve it as a hostname */
|
|
|
|
if ((lAddr == INADDR_NONE) && (strcmp(szHost, "255.255.255.255"))) {
|
|
|
|
|
|
|
|
lpstHost = gethostbyname(szHost);
|
|
|
|
if (lpstHost) { /* success */
|
|
|
|
lAddr = *((u_long FAR *) (lpstHost->h_addr));
|
|
|
|
} else {
|
|
|
|
lAddr = INADDR_ANY; /* failure */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (lAddr);
|
2006-01-06 10:04:33 +08:00
|
|
|
} /* end GetAddr() */
|
2009-08-19 02:58:33 +08:00
|
|
|
|
2021-03-01 23:18:40 +08:00
|
|
|
/* returns the contents of an angle-addr (caller needs to efree) or NULL */
|
|
|
|
static char *get_angle_addr(char *address)
|
|
|
|
{
|
2021-06-18 19:25:39 +08:00
|
|
|
bool in_quotes = 0;
|
2021-03-01 23:18:40 +08:00
|
|
|
char *p1 = address, *p2;
|
|
|
|
|
|
|
|
while ((p1 = strpbrk(p1, "<\"\\")) != NULL) {
|
|
|
|
if (*p1 == '\\' && in_quotes) {
|
|
|
|
if (p1[1] == '\0') {
|
|
|
|
/* invalid address; let SMTP server deal with it */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
p1++;
|
|
|
|
} else if (*p1 == '"') {
|
|
|
|
in_quotes = !in_quotes;
|
|
|
|
} else if (*p1 == '<' && !in_quotes) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p1++;
|
|
|
|
}
|
|
|
|
if (p1 == NULL) return NULL;
|
|
|
|
p2 = ++p1;
|
|
|
|
while ((p2 = strpbrk(p2, ">\"\\")) != NULL) {
|
|
|
|
if (*p2 == '\\' && in_quotes) {
|
|
|
|
if (p2[1] == '\0') {
|
|
|
|
/* invalid address; let SMTP server deal with it */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
p2++;
|
|
|
|
} else if (*p2 == '"') {
|
|
|
|
in_quotes = !in_quotes;
|
|
|
|
} else if (*p2 == '>' && !in_quotes) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p2++;
|
|
|
|
}
|
|
|
|
if (p2 == NULL) return NULL;
|
|
|
|
|
|
|
|
return estrndup(p1, p2 - p1);
|
|
|
|
}
|
2009-08-19 02:58:33 +08:00
|
|
|
|
2024-08-10 19:59:06 +08:00
|
|
|
//*********************************************************************
|
2009-08-19 02:58:33 +08:00
|
|
|
// Name: int FormatEmailAddress
|
2015-01-03 17:22:58 +08:00
|
|
|
// Input:
|
2009-08-19 02:58:33 +08:00
|
|
|
// Output:
|
2020-01-16 19:04:00 +08:00
|
|
|
// Description: Formats the email address to remove any content outside
|
2009-08-19 02:58:33 +08:00
|
|
|
// of the angle brackets < > as per RFC 2821.
|
|
|
|
//
|
2015-01-03 17:22:58 +08:00
|
|
|
// Returns the invalidly formatted mail address if the < > are
|
2009-08-19 02:58:33 +08:00
|
|
|
// unbalanced (the SMTP server should reject it if it's out of spec.)
|
2015-01-03 17:22:58 +08:00
|
|
|
//
|
2009-08-19 02:58:33 +08:00
|
|
|
// Author/Date: garretts 08/18/2009
|
|
|
|
// History:
|
2024-08-10 19:59:06 +08:00
|
|
|
//*********************************************************************
|
2009-08-27 05:59:54 +08:00
|
|
|
static int FormatEmailAddress(char* Buf, char* EmailAddress, char* FormatString) {
|
2021-03-01 23:18:40 +08:00
|
|
|
char *tmpAddress;
|
2009-08-19 02:58:33 +08:00
|
|
|
int result;
|
|
|
|
|
2021-03-01 23:18:40 +08:00
|
|
|
if ((tmpAddress = get_angle_addr(EmailAddress)) != NULL) {
|
|
|
|
result = snprintf(Buf, MAIL_BUFFER_SIZE, FormatString, tmpAddress);
|
|
|
|
efree(tmpAddress);
|
2009-08-19 02:58:33 +08:00
|
|
|
return result;
|
2015-01-03 17:22:58 +08:00
|
|
|
}
|
2009-08-25 20:47:28 +08:00
|
|
|
return snprintf(Buf, MAIL_BUFFER_SIZE , FormatString , EmailAddress );
|
2009-08-19 02:58:33 +08:00
|
|
|
} /* end FormatEmailAddress() */
|