1999-04-08 05:05:13 +08:00
|
|
|
/*
|
|
|
|
* PHP Sendmail for Windows.
|
|
|
|
*
|
|
|
|
* This file is rewriten specificly for PHPFI. Some functionality
|
|
|
|
* has been removed (MIME and file attachments). This code was
|
|
|
|
* modified from code based on code writen by Jarle Aase.
|
|
|
|
*
|
|
|
|
* This class is based on the original code by Jarle Aase, see bellow:
|
|
|
|
* 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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2001-12-21 02:06:13 +08:00
|
|
|
/* $Id$ */
|
|
|
|
|
1999-04-08 05:05:13 +08:00
|
|
|
#include "php.h" /*php specific */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <winsock.h>
|
|
|
|
#include "time.h"
|
|
|
|
#include <string.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <memory.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
|
|
|
|
|
|
|
/*
|
|
|
|
extern int _daylight;
|
|
|
|
extern long _timezone;
|
|
|
|
*/
|
|
|
|
/*enum
|
|
|
|
{
|
|
|
|
DO_CONNECT = WM_USER +1
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
|
|
|
static char *days[] =
|
|
|
|
{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
|
|
|
|
static char *months[] =
|
|
|
|
{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
|
|
|
|
|
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"
|
|
|
|
/* Convinient way to handle error messages from the SMTP server.
|
|
|
|
response is ecalloc()d in Ack() itself and efree()d here
|
|
|
|
because the content is in *error_message now */
|
|
|
|
#define SMTP_ERROR_RESPONSE(response) { \
|
|
|
|
if (NULL != (*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); \
|
|
|
|
} \
|
|
|
|
efree(response); \
|
|
|
|
}
|
|
|
|
|
1999-04-08 05:05:13 +08:00
|
|
|
#ifndef THREAD_SAFE
|
|
|
|
char Buffer[MAIL_BUFFER_SIZE];
|
|
|
|
|
2000-06-17 02:24:02 +08:00
|
|
|
/* socket related data */
|
1999-04-08 05:05:13 +08:00
|
|
|
SOCKET sc;
|
|
|
|
WSADATA Data;
|
|
|
|
struct hostent *adr;
|
|
|
|
SOCKADDR_IN sock_in;
|
|
|
|
int WinsockStarted;
|
2000-06-17 02:24:02 +08:00
|
|
|
/* values set by the constructor */
|
1999-04-08 05:05:13 +08:00
|
|
|
char *AppName;
|
|
|
|
char MailHost[HOST_NAME_LEN];
|
|
|
|
char LocalHost[HOST_NAME_LEN];
|
|
|
|
#endif
|
|
|
|
char seps[] = " ,\t\n";
|
2002-05-15 04:40:39 +08:00
|
|
|
char *php_mailer = "PHP 4 WIN32";
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
char *get_header(char *h, char *headers);
|
|
|
|
|
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-05-14 21:52:18 +08:00
|
|
|
{"Mailserver rejected our \"sendmail_from\" setting"} /* 20 */
|
1999-04-08 05:05:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-17 02:24:02 +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
|
|
|
|
// 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!
|
2000-06-17 02:24:02 +08:00
|
|
|
//********************************************************************/
|
2002-05-15 04:40:39 +08:00
|
|
|
int TSendMail(char *host, int *error, char **error_message,
|
1999-04-08 05:05:13 +08:00
|
|
|
char *headers, char *Subject, char *mailTo, char *data)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char *RPath = NULL;
|
2002-05-18 00:16:27 +08:00
|
|
|
char *headers_lc = NULL; /* headers_lc is only created if we've a header at all */
|
1999-04-08 05:05:13 +08:00
|
|
|
|
1999-04-24 08:12:00 +08:00
|
|
|
WinsockStarted = FALSE;
|
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 {
|
1999-04-24 08:12:00 +08:00
|
|
|
strcpy(MailHost, host);
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
|
|
|
|
2002-05-14 23:29:28 +08:00
|
|
|
if (headers) {
|
|
|
|
char *pos = NULL;
|
2002-05-18 00:16:27 +08:00
|
|
|
size_t i;
|
|
|
|
/* Create a lowercased header for all the searches so we're finally case
|
|
|
|
* insensitive when searching for a pattern. */
|
|
|
|
if (NULL == (headers_lc = estrdup(headers))) {
|
|
|
|
*error = OUT_OF_MEMORY;
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
for (i = 0; i < strlen(headers_lc); i++) {
|
|
|
|
headers_lc[i] = tolower(headers_lc[i]);
|
|
|
|
}
|
2002-05-14 23:29:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fall back to sendmail_from php.ini setting */
|
2002-06-03 06:54:48 +08:00
|
|
|
if (INI_STR("sendmail_from")) {
|
|
|
|
RPath = estrdup(INI_STR("sendmail_from"));
|
|
|
|
} else {
|
|
|
|
if (headers_lc) {
|
|
|
|
efree(headers_lc);
|
2002-05-14 23:29:28 +08:00
|
|
|
}
|
2002-06-03 06:54:48 +08:00
|
|
|
*error = W32_SM_SENDMAIL_FROM_NOT_SET;
|
|
|
|
return FAILURE;
|
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-05-18 00:16:27 +08:00
|
|
|
if (headers_lc) {
|
|
|
|
efree(headers_lc);
|
|
|
|
}
|
2002-05-15 18:11:33 +08:00
|
|
|
/* 128 is safe here, the specifier in snprintf isn't longer than that */
|
2002-05-15 04:40:39 +08:00
|
|
|
if (NULL == (*error_message = ecalloc(1, HOST_NAME_LEN + 128))) {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
2002-06-03 01:13:48 +08:00
|
|
|
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"));
|
2002-05-15 04:40:39 +08:00
|
|
|
return FAILURE;
|
1999-04-08 05:05:13 +08:00
|
|
|
} else {
|
2002-05-18 00:16:27 +08:00
|
|
|
ret = SendText(RPath, Subject, mailTo, data, headers, headers_lc, error_message);
|
1999-04-08 05:05:13 +08:00
|
|
|
TSMClose();
|
2002-05-15 04:40:39 +08:00
|
|
|
if (RPath) {
|
|
|
|
efree(RPath);
|
|
|
|
}
|
2002-05-18 00:16:27 +08:00
|
|
|
if (headers_lc) {
|
|
|
|
efree(headers_lc);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-06-17 22:48:03 +08:00
|
|
|
//********************************************************************
|
1999-04-08 05:05:13 +08:00
|
|
|
// Name: TSendMail::~TSendMail
|
|
|
|
// Input:
|
|
|
|
// Output:
|
|
|
|
// Description: DESTRUCTOR
|
|
|
|
// Author/Date: jcar 20/9/96
|
|
|
|
// History:
|
2000-06-17 02:24:02 +08:00
|
|
|
//********************************************************************/
|
1999-04-08 05:05:13 +08:00
|
|
|
void TSMClose()
|
|
|
|
{
|
2000-07-10 22:49:20 +08:00
|
|
|
Post("QUIT\r\n");
|
2002-05-15 04:40:39 +08:00
|
|
|
Ack(NULL);
|
2000-06-17 02:24:02 +08:00
|
|
|
/* to guarantee that the cleanup is not made twice and
|
|
|
|
compomise the rest of the application if sockets are used
|
|
|
|
elesewhere
|
|
|
|
*/
|
2000-07-28 20:21:44 +08:00
|
|
|
|
|
|
|
shutdown(sc, 0);
|
|
|
|
closesocket(sc);
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-17 02:24:02 +08:00
|
|
|
/*********************************************************************
|
1999-04-08 05:05:13 +08:00
|
|
|
// Name: char *GetSMErrorText
|
|
|
|
// Input: Error index returned by the menber functions
|
|
|
|
// Output: pointer to a string containing the error description
|
|
|
|
// Description:
|
|
|
|
// Author/Date: jcar 20/9/96
|
|
|
|
// History:
|
2000-06-17 02:24:02 +08:00
|
|
|
//*******************************************************************/
|
1999-04-08 05:05:13 +08:00
|
|
|
char *GetSMErrorText(int index)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-17 02:24:02 +08:00
|
|
|
/*********************************************************************
|
1999-04-08 05:05:13 +08:00
|
|
|
// 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.
|
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:
|
2000-06-17 02:24:02 +08:00
|
|
|
//*******************************************************************/
|
2002-05-18 00:16:27 +08:00
|
|
|
int SendText(char *RPath, char *Subject, char *mailTo, char *data, char *headers, char *headers_lc, char **error_message)
|
1999-04-08 05:05:13 +08:00
|
|
|
{
|
|
|
|
int res, i;
|
|
|
|
char *p;
|
2000-07-10 22:49:20 +08:00
|
|
|
char *tempMailTo, *token, *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;
|
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
|
|
|
|
2000-07-10 22:49:20 +08:00
|
|
|
sprintf(Buffer, "HELO %s\r\n", LocalHost);
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2000-06-17 02:24:02 +08:00
|
|
|
/* in the beggining of the dialog */
|
|
|
|
/* attempt reconnect if the first Post fail */
|
1999-04-24 08:12:00 +08:00
|
|
|
if ((res = Post(Buffer)) != SUCCESS) {
|
1999-04-08 05:05:13 +08:00
|
|
|
MailConnect();
|
1999-04-24 08:12:00 +08:00
|
|
|
if ((res = Post(Buffer)) != SUCCESS)
|
1999-04-08 05:05:13 +08:00
|
|
|
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
|
|
|
|
2002-06-03 06:10:25 +08:00
|
|
|
snprintf(Buffer, MAIL_BUFFER_SIZE, "MAIL FROM:<%s>\r\n", RPath);
|
2000-06-28 02:41:44 +08:00
|
|
|
if ((res = Post(Buffer)) != SUCCESS)
|
|
|
|
return (res);
|
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 */
|
|
|
|
token = strtok(tempMailTo, ",");
|
|
|
|
while(token != NULL)
|
|
|
|
{
|
2002-06-03 06:10:25 +08:00
|
|
|
snprintf(Buffer, MAIL_BUFFER_SIZE, "RCPT TO:<%s>\r\n", token);
|
2000-07-10 22:52:17 +08:00
|
|
|
if ((res = Post(Buffer)) != SUCCESS)
|
2000-07-10 22:49:20 +08:00
|
|
|
return (res);
|
2002-05-15 04:40:39 +08:00
|
|
|
if ((res = Ack(&server_response)) != SUCCESS) {
|
|
|
|
SMTP_ERROR_RESPONSE(server_response);
|
2000-07-10 22:49:20 +08:00
|
|
|
return (res);
|
2002-05-15 04:40:39 +08:00
|
|
|
}
|
2000-07-10 22:49:20 +08:00
|
|
|
token = strtok(NULL, ",");
|
|
|
|
}
|
2002-05-15 18:11:33 +08:00
|
|
|
efree(tempMailTo);
|
2000-07-10 22:49:20 +08:00
|
|
|
|
|
|
|
/* Send mail to all Cc rcpt's */
|
2002-05-18 00:16:27 +08:00
|
|
|
if (headers && (pos1 = strstr(headers_lc, "cc:"))) {
|
|
|
|
/* 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;
|
2002-05-14 21:52:18 +08:00
|
|
|
if (NULL == (pos2 = strstr(pos1, "\r\n"))) {
|
2002-05-14 23:32:15 +08:00
|
|
|
|
2002-05-14 21:52:18 +08:00
|
|
|
tempMailTo = estrndup(pos1, strlen(pos1));
|
2002-05-14 23:32:15 +08:00
|
|
|
|
2002-05-14 21:52:18 +08:00
|
|
|
} else {
|
|
|
|
tempMailTo = estrndup(pos1, pos2-pos1);
|
2002-05-14 23:32:15 +08:00
|
|
|
|
2002-05-14 21:52:18 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2000-07-10 22:49:20 +08:00
|
|
|
token = strtok(tempMailTo, ",");
|
|
|
|
while(token != NULL)
|
|
|
|
{
|
|
|
|
sprintf(Buffer, "RCPT TO:<%s>\r\n", token);
|
|
|
|
if ((res = Post(Buffer)) != SUCCESS)
|
|
|
|
return (res);
|
2002-05-15 04:40:39 +08:00
|
|
|
if ((res = Ack(&server_response)) != SUCCESS) {
|
|
|
|
SMTP_ERROR_RESPONSE(server_response);
|
2000-07-10 22:49:20 +08:00
|
|
|
return (res);
|
2002-05-15 04:40:39 +08:00
|
|
|
}
|
2000-07-10 22:49:20 +08:00
|
|
|
token = strtok(NULL, ",");
|
|
|
|
}
|
|
|
|
efree(tempMailTo);
|
|
|
|
}
|
|
|
|
|
2002-05-15 18:11:33 +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. */
|
2002-05-18 00:16:27 +08:00
|
|
|
if (headers) {
|
|
|
|
if (pos1 = strstr(headers_lc, "bcc:")) {
|
|
|
|
/* 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"))) {
|
|
|
|
int foo = strlen(pos1);
|
|
|
|
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);
|
2002-05-15 18:11:33 +08:00
|
|
|
}
|
2002-05-18 00:16:27 +08:00
|
|
|
|
|
|
|
token = strtok(tempMailTo, ",");
|
|
|
|
while(token != NULL)
|
|
|
|
{
|
|
|
|
sprintf(Buffer, "RCPT TO:<%s>\r\n", token);
|
|
|
|
if ((res = Post(Buffer)) != SUCCESS) {
|
|
|
|
return (res);
|
|
|
|
}
|
|
|
|
if ((res = Ack(&server_response)) != SUCCESS) {
|
|
|
|
SMTP_ERROR_RESPONSE(server_response);
|
|
|
|
return (res);
|
|
|
|
}
|
|
|
|
token = strtok(NULL, ",");
|
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. */
|
|
|
|
if (NULL == (stripped_header = ecalloc(1, strlen(headers)))) {
|
|
|
|
return OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
/* 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 substract 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);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* 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 (NULL == (stripped_header = estrndup(headers, strlen(headers)))) {
|
|
|
|
return OUT_OF_MEMORY;
|
|
|
|
}
|
2002-05-15 18:11:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((res = Post("DATA\r\n")) != SUCCESS) {
|
|
|
|
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);
|
2002-05-15 18:11:33 +08:00
|
|
|
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) {
|
|
|
|
res = PostHeader(RPath, "No Subject", mailTo, stripped_header, NULL);
|
|
|
|
} else {
|
|
|
|
res = PostHeader(RPath, Subject, mailTo, stripped_header, NULL);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
2000-06-17 02:24:02 +08:00
|
|
|
/* send message contents in 1024 chunks */
|
1999-04-08 05:05:13 +08:00
|
|
|
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);
|
|
|
|
|
2000-06-17 02:24:02 +08:00
|
|
|
/* put next chunk in buffer */
|
1999-04-24 08:12:00 +08:00
|
|
|
strncpy(Buffer, p, i);
|
|
|
|
Buffer[i] = '\0';
|
1999-04-08 05:05:13 +08:00
|
|
|
p += i;
|
|
|
|
|
2000-06-17 02:24:02 +08:00
|
|
|
/* send chunk */
|
1999-04-24 08:12:00 +08:00
|
|
|
if ((res = Post(Buffer)) != SUCCESS)
|
1999-04-08 05:05:13 +08:00
|
|
|
return (res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2002-06-03 06:10:25 +08:00
|
|
|
int addToHeader(char **header_buffer, const char *specifier, char *string) {
|
|
|
|
if (NULL == (*header_buffer = erealloc(*header_buffer, strlen(*header_buffer) + strlen(specifier) + strlen(string) + 1))) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
sprintf(*header_buffer + strlen(*header_buffer), specifier, string);
|
|
|
|
return 1;
|
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2000-06-17 02:24:02 +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
|
|
|
|
// 5) cc destination address
|
1999-04-08 05:05:13 +08:00
|
|
|
// Output: Error code or Success
|
|
|
|
// Description:
|
|
|
|
// Author/Date: jcar 20/9/96
|
|
|
|
// History:
|
2000-06-17 02:24:02 +08:00
|
|
|
//********************************************************************/
|
2001-12-05 02:47:07 +08:00
|
|
|
int PostHeader(char *RPath, char *Subject, char *mailTo, char *xheaders, char *mailCc)
|
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
|
|
|
|
|
|
|
time_t tNow = time(NULL);
|
|
|
|
struct tm *tm = localtime(&tNow);
|
|
|
|
int zoneh = abs(_timezone);
|
|
|
|
int zonem, 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) {
|
|
|
|
if (NULL == (headers_lc = estrdup(xheaders))) {
|
|
|
|
return OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
for (i = 0; i < strlen(headers_lc); i++) {
|
|
|
|
headers_lc[i] = tolower(headers_lc[i]);
|
|
|
|
}
|
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2002-06-03 06:10:25 +08:00
|
|
|
if (NULL == (header_buffer = ecalloc(1, MAIL_BUFFER_SIZE))) {
|
|
|
|
efree(headers_lc);
|
|
|
|
return OUT_OF_MEMORY;
|
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
zoneh /= (60 * 60);
|
|
|
|
zonem = (abs(_timezone) / 60) - (zoneh * 60);
|
|
|
|
|
2002-05-18 00:16:27 +08:00
|
|
|
if(!xheaders || !strstr(headers_lc, "date:")){
|
2002-06-03 06:10:25 +08:00
|
|
|
sprintf(header_buffer, "Date: %s, %02d %s %04d %02d:%02d:%02d %s%02d%02d\r\n",
|
1999-06-10 17:10:07 +08:00
|
|
|
days[tm->tm_wday],
|
|
|
|
tm->tm_mday,
|
|
|
|
months[tm->tm_mon],
|
|
|
|
tm->tm_year + 1900,
|
|
|
|
tm->tm_hour,
|
|
|
|
tm->tm_min,
|
|
|
|
tm->tm_sec,
|
2001-11-30 17:29:15 +08:00
|
|
|
(_timezone <= 0) ? "+" : (_timezone > 0) ? "-" : "",
|
1999-06-10 17:10:07 +08:00
|
|
|
zoneh,
|
|
|
|
zonem);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2002-06-03 07:39:59 +08:00
|
|
|
|
2001-12-05 07:33:52 +08:00
|
|
|
if (mailCc && *mailCc) {
|
2002-06-03 06:10:25 +08:00
|
|
|
if (!addToHeader(&header_buffer, "Cc: %s\r\n", mailCc)) {
|
|
|
|
goto PostHeader_outofmem;
|
|
|
|
}
|
2001-12-05 07:33:52 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
if(xheaders){
|
2002-06-03 06:10:25 +08:00
|
|
|
if (!addToHeader(&header_buffer, "%s", xheaders)) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-17 02:24:02 +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:
|
2000-06-17 02:24:02 +08:00
|
|
|
//********************************************************************/
|
1999-04-08 05:05:13 +08:00
|
|
|
int MailConnect()
|
|
|
|
{
|
|
|
|
|
|
|
|
int res;
|
2000-08-07 05:42:10 +08:00
|
|
|
short portnum;
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2000-06-17 02:24:02 +08:00
|
|
|
/* Create Socket */
|
1999-04-24 08:12:00 +08:00
|
|
|
if ((sc = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
|
1999-04-08 05:05:13 +08:00
|
|
|
return (FAILED_TO_OBTAIN_SOCKET_HANDLE);
|
|
|
|
|
2000-06-17 02:24:02 +08:00
|
|
|
/* Get our own host name */
|
1999-04-24 08:12:00 +08:00
|
|
|
if (gethostname(LocalHost, HOST_NAME_LEN))
|
1999-04-08 05:05:13 +08:00
|
|
|
return (FAILED_TO_GET_HOSTNAME);
|
|
|
|
|
2000-06-17 02:24:02 +08:00
|
|
|
/* Resolve the servers IP */
|
|
|
|
/*
|
|
|
|
if (!isdigit(MailHost[0])||!gethostbyname(MailHost))
|
|
|
|
{
|
|
|
|
return (FAILED_TO_RESOLVE_HOST);
|
|
|
|
}
|
|
|
|
*/
|
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);
|
1999-04-24 08:12:00 +08:00
|
|
|
sock_in.sin_addr.S_un.S_addr = GetAddr(MailHost);
|
1999-04-08 05:05:13 +08:00
|
|
|
|
1999-04-24 08:12:00 +08:00
|
|
|
if (connect(sc, (LPSOCKADDR) & sock_in, sizeof(sock_in)))
|
1999-04-08 05:05:13 +08:00
|
|
|
return (FAILED_TO_CONNECT);
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-17 02:24:02 +08:00
|
|
|
/*********************************************************************
|
1999-04-08 05:05:13 +08:00
|
|
|
// Name: Post
|
|
|
|
// Input:
|
|
|
|
// Output:
|
|
|
|
// Description:
|
|
|
|
// Author/Date: jcar 20/9/96
|
|
|
|
// History:
|
2000-06-17 02:24:02 +08:00
|
|
|
//********************************************************************/
|
1999-04-08 05:05:13 +08:00
|
|
|
int Post(LPCSTR msg)
|
|
|
|
{
|
|
|
|
int len = strlen(msg);
|
|
|
|
int slen;
|
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
while (len > 0) {
|
1999-04-24 08:12:00 +08:00
|
|
|
if ((slen = send(sc, msg + index, len, 0)) < 1)
|
1999-04-08 05:05:13 +08:00
|
|
|
return (FAILED_TO_SEND);
|
|
|
|
len -= slen;
|
|
|
|
index += slen;
|
|
|
|
}
|
|
|
|
return (SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-17 02:24:02 +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:
|
2000-06-17 02:24:02 +08:00
|
|
|
//********************************************************************/
|
2002-05-15 04:40:39 +08:00
|
|
|
int Ack(char **server_response)
|
1999-04-08 05:05:13 +08:00
|
|
|
{
|
2002-05-15 04:40:39 +08:00
|
|
|
static char buf[MAIL_BUFFER_SIZE];
|
1999-04-08 05:05:13 +08:00
|
|
|
int rlen;
|
|
|
|
int Index = 0;
|
|
|
|
int Received = 0;
|
|
|
|
|
|
|
|
again:
|
|
|
|
|
2002-05-15 04:40:39 +08:00
|
|
|
if ((rlen = recv(sc, buf + Index, ((MAIL_BUFFER_SIZE) - 1) - Received, 0)) < 1)
|
1999-04-08 05:05:13 +08:00
|
|
|
return (FAILED_TO_RECEIVE);
|
|
|
|
|
|
|
|
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;
|
2000-09-05 08:26:15 +08:00
|
|
|
|
|
|
|
if ((buf[Received - 4] == ' ' && buf[Received - 3] == '-') ||
|
|
|
|
(buf[Received - 2] != '\r') || (buf[Received - 1] != '\n'))
|
2000-06-17 02:24:02 +08:00
|
|
|
/* err_msg fprintf(stderr,"Incomplete server message. Awaiting CRLF\n"); */
|
2000-09-05 08:26:15 +08:00
|
|
|
goto again; /* Incomplete data. Line must be terminated by CRLF
|
|
|
|
And not contain a space followed by a '-' */
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-17 02:24:02 +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:
|
2000-06-17 02:24:02 +08:00
|
|
|
//********************************************************************/
|
1999-04-08 05:05:13 +08:00
|
|
|
unsigned long GetAddr(LPSTR szHost)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
} /* end GetAddr() */
|