winex11.drv: Fix handle leak.
[wine.git] / dlls / winemapi / sendmail.c
blobe017e2c000bab931c82622471478276f2e762755
1 /*
2 * MAPISendMail implementation
4 * Copyright 2005 Hans Leidekker
5 * Copyright 2009 Owen Rudge for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdio.h>
26 #include <stdarg.h>
28 #define COBJMACROS
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winerror.h"
33 #include "mapi.h"
34 #include "winreg.h"
35 #include "shellapi.h"
36 #include "shlwapi.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(winemapi);
41 /* Escapes a string for use in mailto: URL */
42 static char *escape_string(char *in, char *empty_string)
44 HRESULT res;
45 DWORD size;
46 char *escaped = NULL;
48 if (!in)
49 return empty_string;
51 size = 1;
52 res = UrlEscapeA(in, empty_string, &size, URL_ESCAPE_PERCENT | URL_ESCAPE_SEGMENT_ONLY);
54 if (res == E_POINTER)
56 escaped = HeapAlloc(GetProcessHeap(), 0, size);
58 if (!escaped)
59 return in;
61 /* If for some reason UrlEscape fails, just send the original text */
62 if (UrlEscapeA(in, escaped, &size, URL_ESCAPE_PERCENT | URL_ESCAPE_SEGMENT_ONLY) != S_OK)
64 HeapFree(GetProcessHeap(), 0, escaped);
65 escaped = in;
69 return escaped ? escaped : empty_string;
72 /**************************************************************************
73 * MAPISendMail
75 * Send a message using a native mail client.
77 * PARAMS
78 * session [I] Handle to a MAPI session.
79 * uiparam [I] Parent window handle.
80 * message [I] Pointer to a MAPIMessage structure.
81 * flags [I] Flags.
82 * reserved [I] Reserved, pass 0.
84 * RETURNS
85 * Success: SUCCESS_SUCCESS
86 * Failure: MAPI_E_FAILURE
89 ULONG WINAPI MAPISendMail(LHANDLE session, ULONG_PTR uiparam,
90 lpMapiMessage message, FLAGS flags, ULONG reserved)
92 ULONG ret = MAPI_E_FAILURE;
93 unsigned int i, to_count = 0, cc_count = 0, bcc_count = 0;
94 unsigned int to_size = 0, cc_size = 0, bcc_size = 0, subj_size, body_size;
96 char *to = NULL, *cc = NULL, *bcc = NULL, *subject = NULL, *body = NULL;
97 const char *address;
98 static const char format[] =
99 "mailto:\"%s\"?subject=\"%s\"&cc=\"%s\"&bcc=\"%s\"&body=\"%s\"";
100 static const char smtp[] = "smtp:";
101 char *mailto = NULL, *escape = NULL;
102 char empty_string[] = "";
103 HRESULT res;
104 DWORD size;
106 TRACE("(0x%08lx 0x%08lx %p 0x%08x 0x%08x)\n", session, uiparam,
107 message, flags, reserved);
109 if (!message)
110 return MAPI_E_FAILURE;
112 for (i = 0; i < message->nRecipCount; i++)
114 if (!message->lpRecips)
116 WARN("No recipients found\n");
117 return MAPI_E_FAILURE;
120 address = message->lpRecips[i].lpszAddress;
122 if (address)
124 if (!strncasecmp(address, smtp, sizeof(smtp) - 1))
125 address += sizeof(smtp) - 1;
127 switch (message->lpRecips[i].ulRecipClass)
129 case MAPI_ORIG:
130 TRACE("From: %s\n", debugstr_a(address));
131 break;
133 case MAPI_TO:
134 TRACE("To: %s\n", debugstr_a(address));
135 to_size += lstrlenA(address) + 1;
136 break;
138 case MAPI_CC:
139 TRACE("Cc: %s\n", debugstr_a(address));
140 cc_size += lstrlenA(address) + 1;
141 break;
143 case MAPI_BCC:
144 TRACE("Bcc: %s\n", debugstr_a(address));
145 bcc_size += lstrlenA(address) + 1;
146 break;
148 default:
149 TRACE("Unknown recipient class: %d\n",
150 message->lpRecips[i].ulRecipClass);
153 else
154 FIXME("Name resolution and entry identifiers not supported\n");
157 if (message->nFileCount)
158 FIXME("Ignoring attachments\n");
160 /* Escape subject and body */
161 subject = escape_string(message->lpszSubject, empty_string);
162 body = escape_string(message->lpszNoteText, empty_string);
164 TRACE("Subject: %s\n", debugstr_a(subject));
165 TRACE("Body: %s\n", debugstr_a(body));
167 subj_size = lstrlenA(subject);
168 body_size = lstrlenA(body);
170 ret = MAPI_E_INSUFFICIENT_MEMORY;
172 if (to_size)
174 to = HeapAlloc(GetProcessHeap(), 0, to_size);
176 if (!to)
177 goto exit;
179 to[0] = 0;
182 if (cc_size)
184 cc = HeapAlloc(GetProcessHeap(), 0, cc_size);
186 if (!cc)
187 goto exit;
189 cc[0] = 0;
192 if (bcc_size)
194 bcc = HeapAlloc(GetProcessHeap(), 0, bcc_size);
196 if (!bcc)
197 goto exit;
199 bcc[0] = 0;
202 if (message->lpOriginator)
203 TRACE("From: %s\n", debugstr_a(message->lpOriginator->lpszAddress));
205 for (i = 0; i < message->nRecipCount; i++)
207 address = message->lpRecips[i].lpszAddress;
209 if (address)
211 if (!strncasecmp(address, smtp, sizeof(smtp) - 1))
212 address += sizeof(smtp) - 1;
214 switch (message->lpRecips[i].ulRecipClass)
216 case MAPI_TO:
217 if (to_count)
218 lstrcatA(to, ",");
220 lstrcatA(to, address);
221 to_count++;
222 break;
224 case MAPI_CC:
225 if (cc_count)
226 lstrcatA(cc, ",");
228 lstrcatA(cc, address);
229 cc_count++;
230 break;
232 case MAPI_BCC:
233 if (bcc_count)
234 lstrcatA(bcc, ",");
236 lstrcatA(bcc, address);
237 bcc_count++;
238 break;
242 ret = MAPI_E_FAILURE;
243 size = sizeof(format) + to_size + cc_size + bcc_size + subj_size + body_size;
245 mailto = HeapAlloc(GetProcessHeap(), 0, size);
247 if (!mailto)
248 goto exit;
250 sprintf(mailto, format, to ? to : "", subject, cc ? cc : "", bcc ? bcc : "", body);
252 size = 1;
253 res = UrlEscapeA(mailto, empty_string, &size, URL_ESCAPE_SPACES_ONLY);
255 if (res != E_POINTER)
256 goto exit;
258 escape = HeapAlloc(GetProcessHeap(), 0, size);
260 if (!escape)
261 goto exit;
263 res = UrlEscapeA(mailto, escape, &size, URL_ESCAPE_SPACES_ONLY);
265 if (res != S_OK)
266 goto exit;
268 TRACE("Executing winebrowser.exe with parameters '%s'\n", debugstr_a(escape));
270 if ((UINT_PTR) ShellExecuteA(NULL, "open", "winebrowser.exe", escape, NULL, 0) > 32)
271 ret = SUCCESS_SUCCESS;
273 exit:
274 HeapFree(GetProcessHeap(), 0, to);
275 HeapFree(GetProcessHeap(), 0, cc);
276 HeapFree(GetProcessHeap(), 0, bcc);
277 HeapFree(GetProcessHeap(), 0, mailto);
278 HeapFree(GetProcessHeap(), 0, escape);
280 if (subject != empty_string)
281 HeapFree(GetProcessHeap(), 0, subject);
283 if (body != empty_string)
284 HeapFree(GetProcessHeap(), 0, body);
286 return ret;
289 ULONG WINAPI MAPISendDocuments(ULONG_PTR uiparam, LPSTR delim, LPSTR paths,
290 LPSTR filenames, ULONG reserved)
292 return MAPI_E_NOT_SUPPORTED;