summaryrefslogtreecommitdiff
path: root/win32/imap_sendmail.c
blob: 1fdb7d0ba91d4772b98802c0ac0b88d4519d652c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
   +----------------------------------------------------------------------+
   | PHP Version 4                                                        |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2002 The PHP Group                                |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.02 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available at through the world-wide-web at                           |
   | http://www.php.net/license/2_02.txt.                                 |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Authors: Frank M. Kromann <frank@kromann.info>                       |
   +----------------------------------------------------------------------+
 */
/* $Id$ */

#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 "ext/imap/php_imap.h"
#include "sendmail.h"
#include "imap_sendmail.h"
#include "php_ini.h"

char Buffer[MAIL_BUFFER_SIZE];

extern int WinsockStarted;
extern char MailHost[HOST_NAME_LEN];
extern char LocalHost[HOST_NAME_LEN];


/********************************************************************
 Name:  imap_TSendMail
 Input:   1) host:    Name of the mail host where the SMTP server resides
                      max accepted length of name = 256
          2) appname: Name of the application to use in the X-mailer
                      field of the message. if NULL is given the application
                      name is used as given by the GetCommandLine() function
                      max accespted length of name = 100
 Output:  1) error:   Returns the error code if something went wrong or
                      SUCCESS otherwise.

 See SendText() for additional args!
********************************************************************/
int imap_TSendMail(char *host, int *error,
			  char *headers, char *Subject, char *mailTo, char *data, char *mailCc, char *mailBcc, char *mailRPath)
{
	int ret;
	char *RPath = NULL;

	WinsockStarted = FALSE;

	if (host == NULL) {
		*error = BAD_MAIL_HOST;
		return BAD_MAIL_HOST;
	} else if (strlen(host) >= HOST_NAME_LEN) {
		*error = BAD_MAIL_HOST;
		return BAD_MAIL_HOST;
	} else {
		strcpy(MailHost, host);
	}

	if (mailRPath)
		RPath = estrdup(mailRPath);
	else if (INI_STR("sendmail_from")){
		RPath = estrdup(INI_STR("sendmail_from"));
	} else {
		return 19;
	}

	// attempt to connect with mail host
	*error = MailConnect();
	if (*error != 0) {
		if(RPath)efree(RPath);
		return *error;
	} else {
		ret = imap_SendText(RPath, Subject, mailTo, data, headers, mailCc, mailBcc);
		TSMClose();
		if (ret != SUCCESS) {
			*error = ret;
		}
		if(RPath)efree(RPath);
		return ret;
	}
}

/********************************************************************
 Name:  imap_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.
			  5) headers: Null terminated string containing extra headers
			  6) mailCc:  Additional destination addresses
			  7) mailBcc: Send the mail to these addresses as well, but do not 
			              list these in the headers
 Output:      Error code or SUCCESS
 Description:
 Author/Date:  fmk/12-13-2001
 History:
********************************************************************/
int imap_SendText(char *RPath, char *Subject, char *mailTo, char *data, char *headers, char *mailCc, char *mailBcc)
{
	int res, i;
	char *p;
	char *tempMailTo;
	ADDRESS *addr;
	char *server_response = NULL;

	/* check for NULL parameters */
	if (data == NULL)
		return (BAD_MSG_CONTENTS);
	if (mailTo == NULL)
		return (BAD_MSG_DESTINATION);
	if (RPath == NULL)
		return (BAD_MSG_RPATH);

	/* simple checks for the mailto address */
	/* have ampersand ? */
	if (strchr(mailTo, '@') == NULL)
		return (BAD_MSG_DESTINATION);

	sprintf(Buffer, "HELO %s\r\n", LocalHost);

	/* in the beggining of the dialog */
	/* attempt reconnect if the first Post fail */
	if ((res = Post(Buffer)) != SUCCESS) {
		MailConnect();
		if ((res = Post(Buffer)) != SUCCESS)
			return (res);
	}
	if ((res = Ack(NULL)) != SUCCESS)
		return (res);

	sprintf(Buffer, "MAIL FROM:<%s>\r\n", RPath);
	if ((res = Post(Buffer)) != SUCCESS)
		return (res);
	if ((res = Ack(NULL)) != SUCCESS)
		return (res);

	// Send mail to all rcpt's
	tempMailTo = estrdup(mailTo);	// rfc822_parse_adrlist will change the string
	addr = NULL;
	rfc822_parse_adrlist(&addr, tempMailTo, NULL);
	while (addr) {
		if (strcmp(addr->host, ERRHOST) == 0)
			return (BAD_MSG_DESTINATION);
		else {
			sprintf(Buffer, "RCPT TO:<%s@%s>\r\n", addr->mailbox, addr->host);
			if ((res = Post(Buffer)) != SUCCESS)
				return (res);
			if ((res = Ack(NULL)) != SUCCESS)
				return (res);
		}
		addr = addr->next;
	}
	efree(tempMailTo);

	/* Send mail to all Cc rcpt's */
	if (mailCc && *mailCc) {
		tempMailTo = estrdup(mailCc);
		addr = NULL;
		rfc822_parse_adrlist(&addr, tempMailTo, NULL);
		while (addr) {
			if (strcmp(addr->host, ERRHOST) == 0)
				return (BAD_MSG_DESTINATION);
			else {
				sprintf(Buffer, "RCPT TO:<%s@%s>\r\n", addr->mailbox, addr->host);
				if ((res = Post(Buffer)) != SUCCESS)
					return (res);
				if ((res = Ack(NULL)) != SUCCESS)
					return (res);
			}
			addr = addr->next;
		}
		efree(tempMailTo);
	}

	if (mailBcc && *mailBcc) {
		tempMailTo = estrdup(mailBcc);
		addr = NULL;
		rfc822_parse_adrlist(&addr, tempMailTo, NULL);
		while (addr) {
			if (strcmp(addr->host, ERRHOST) == 0)
				return (BAD_MSG_DESTINATION);
			else {
				sprintf(Buffer, "RCPT TO:<%s@%s>\r\n", addr->mailbox, addr->host);
				if ((res = Post(Buffer)) != SUCCESS)
					return (res);
				if ((res = Ack(NULL)) != SUCCESS)
					return (res);
			}
			addr = addr->next;
		}
		efree(tempMailTo);
	}

	if ((res = Post("DATA\r\n")) != SUCCESS)
		return (res);
	if ((res = Ack(NULL)) != SUCCESS)
		return (res);


	// send message header
	if (Subject == NULL)
		res = PostHeader(RPath, "No Subject", mailTo, headers, mailCc);
	else
		res = PostHeader(RPath, Subject, mailTo, headers, mailCc);
	if (res != SUCCESS)
		return (res);


	/* send message contents in 1024 chunks */
	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);

			/* put next chunk in buffer */
			strncpy(Buffer, p, i);
			Buffer[i] = '\0';
			p += i;

			/* send chunk */
			if ((res = Post(Buffer)) != SUCCESS)
				return (res);
		}
	}

	/*send termination dot */
	if ((res = Post("\r\n.\r\n")) != SUCCESS)
		return (res);
	if ((res = Ack(NULL)) != SUCCESS)
		return (res);

	return (SUCCESS);
}