wininet/tests: Don't call GetLastError() twice.
[wine] / dlls / winemapi / sendmail.c
1 /*
2  * MAPISendMail implementation
3  *
4  * Copyright 2005 Hans Leidekker
5  * Copyright 2009 Owen Rudge for CodeWeavers
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <stdio.h>
26 #include <stdarg.h>
27
28 #define COBJMACROS
29
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"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(winemapi);
40
41 /**************************************************************************
42  *  MAPISendMail
43  *
44  * Send a message using a native mail client.
45  *
46  * PARAMS
47  *  session  [I] Handle to a MAPI session.
48  *  uiparam  [I] Parent window handle.
49  *  message  [I] Pointer to a MAPIMessage structure.
50  *  flags    [I] Flags.
51  *  reserved [I] Reserved, pass 0.
52  *
53  * RETURNS
54  *  Success: SUCCESS_SUCCESS
55  *  Failure: MAPI_E_FAILURE
56  *
57  */
58 ULONG WINAPI MAPISendMail(LHANDLE session, ULONG_PTR uiparam,
59     lpMapiMessage message, FLAGS flags, ULONG reserved)
60 {
61     ULONG ret = MAPI_E_FAILURE;
62     unsigned int i, to_count = 0, cc_count = 0, bcc_count = 0;
63     unsigned int to_size = 0, cc_size = 0, bcc_size = 0, subj_size, body_size;
64
65     char *to = NULL, *cc = NULL, *bcc = NULL;
66     const char *address, *subject, *body;
67     static const char format[] =
68         "mailto:\"%s\"?subject=\"%s\"&cc=\"%s\"&bcc=\"%s\"&body=\"%s\"";
69     char *mailto = NULL, *escape = NULL;
70     char empty_string[] = "";
71     HRESULT res;
72     DWORD size;
73
74     TRACE("(0x%08lx 0x%08lx %p 0x%08x 0x%08x)\n", session, uiparam,
75            message, flags, reserved);
76
77     if (!message)
78         return MAPI_E_FAILURE;
79
80     for (i = 0; i < message->nRecipCount; i++)
81     {
82         if (!message->lpRecips)
83         {
84             WARN("No recipients found\n");
85             return MAPI_E_FAILURE;
86         }
87
88         address = message->lpRecips[i].lpszAddress;
89
90         if (address)
91         {
92             switch (message->lpRecips[i].ulRecipClass)
93             {
94                 case MAPI_ORIG:
95                     TRACE("From: %s\n", debugstr_a(address));
96                     break;
97
98                 case MAPI_TO:
99                     TRACE("To: %s\n", debugstr_a(address));
100                     to_size += lstrlenA(address) + 1;
101                     break;
102
103                 case MAPI_CC:
104                     TRACE("Cc: %s\n", debugstr_a(address));
105                     cc_size += lstrlenA(address) + 1;
106                     break;
107
108                 case MAPI_BCC:
109                     TRACE("Bcc: %s\n", debugstr_a(address));
110                     bcc_size += lstrlenA(address) + 1;
111                     break;
112
113                 default:
114                     TRACE("Unknown recipient class: %d\n",
115                            message->lpRecips[i].ulRecipClass);
116             }
117         }
118         else
119             FIXME("Name resolution and entry identifiers not supported\n");
120     }
121
122     if (message->nFileCount)
123         FIXME("Ignoring attachments\n");
124
125     subject = message->lpszSubject ? message->lpszSubject : "";
126     body = message->lpszNoteText ? message->lpszNoteText : "";
127
128     TRACE("Subject: %s\n", debugstr_a(subject));
129     TRACE("Body: %s\n", debugstr_a(body));
130
131     subj_size = lstrlenA(subject);
132     body_size = lstrlenA(body);
133
134     ret = MAPI_E_INSUFFICIENT_MEMORY;
135
136     if (to_size)
137     {
138         to = HeapAlloc(GetProcessHeap(), 0, to_size);
139
140         if (!to)
141             goto exit;
142
143         to[0] = 0;
144     }
145
146     if (cc_size)
147     {
148         cc = HeapAlloc(GetProcessHeap(), 0, cc_size);
149
150         if (!cc)
151             goto exit;
152
153         cc[0] = 0;
154     }
155
156     if (bcc_size)
157     {
158         bcc = HeapAlloc(GetProcessHeap(), 0, bcc_size);
159
160         if (!bcc)
161             goto exit;
162
163         bcc[0] = 0;
164     }
165
166     if (message->lpOriginator)
167         TRACE("From: %s\n", debugstr_a(message->lpOriginator->lpszAddress));
168
169     for (i = 0; i < message->nRecipCount; i++)
170     {
171         address = message->lpRecips[i].lpszAddress;
172
173         if (address)
174         {
175             switch (message->lpRecips[i].ulRecipClass)
176             {
177                 case MAPI_TO:
178                     if (to_count)
179                         lstrcatA(to, ",");
180
181                     lstrcatA(to, address);
182                     to_count++;
183                     break;
184
185                 case MAPI_CC:
186                     if (cc_count)
187                         lstrcatA(cc, ",");
188
189                     lstrcatA(cc, address);
190                     cc_count++;
191                     break;
192
193                 case MAPI_BCC:
194                     if (bcc_count)
195                         lstrcatA(bcc, ",");
196
197                     lstrcatA(bcc, address);
198                     bcc_count++;
199                     break;
200             }
201         }
202     }
203     ret = MAPI_E_FAILURE;
204     size = sizeof(format) + to_size + cc_size + bcc_size + subj_size + body_size;
205
206     mailto = HeapAlloc(GetProcessHeap(), 0, size);
207
208     if (!mailto)
209         goto exit;
210
211     sprintf(mailto, format, to ? to : "", subject, cc ? cc : "", bcc ? bcc : "", body);
212
213     size = 1;
214     res = UrlEscapeA(mailto, empty_string, &size, URL_ESCAPE_SPACES_ONLY);
215
216     if (res != E_POINTER)
217         goto exit;
218
219     escape = HeapAlloc(GetProcessHeap(), 0, size);
220
221     if (!escape)
222         goto exit;
223
224     res = UrlEscapeA(mailto, escape, &size, URL_ESCAPE_SPACES_ONLY);
225
226     if (res != S_OK)
227         goto exit;
228
229     TRACE("Executing winebrowser.exe with parameters '%s'\n", debugstr_a(escape));
230
231     if ((UINT_PTR) ShellExecuteA(NULL, "open", "winebrowser.exe", escape, NULL, 0) > 32)
232         ret = SUCCESS_SUCCESS;
233
234 exit:
235     HeapFree(GetProcessHeap(), 0, to);
236     HeapFree(GetProcessHeap(), 0, cc);
237     HeapFree(GetProcessHeap(), 0, bcc);
238     HeapFree(GetProcessHeap(), 0, mailto);
239     HeapFree(GetProcessHeap(), 0, escape);
240
241     return ret;
242 }
243
244 ULONG WINAPI MAPISendDocuments(ULONG_PTR uiparam, LPSTR delim, LPSTR paths,
245     LPSTR filenames, ULONG reserved)
246 {
247     return MAPI_E_NOT_SUPPORTED;
248 }