mapi32: Pass through MAPISendMail if Simple MAPI provider loaded.
[wine.git] / dlls / mapi32 / sendmail.c
blob7cf8ec8db8ea3e7db78107080e8fa3ba52c9b505
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 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "objbase.h"
32 #include "mapi.h"
33 #include "winreg.h"
34 #include "shellapi.h"
35 #include "shlwapi.h"
36 #include "wine/debug.h"
37 #include "util.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(mapi);
41 /**************************************************************************
42 * MAPISendMail (MAPI32.211)
44 * Send a mail.
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.
53 * RETURNS
54 * Success: SUCCESS_SUCCESS
55 * Failure: MAPI_E_FAILURE
57 * NOTES
58 * The fallback procedure is a temporary hack.
60 ULONG WINAPI MAPISendMail( LHANDLE session, ULONG_PTR uiparam,
61 lpMapiMessage message, FLAGS flags, ULONG reserved )
63 ULONG ret = MAPI_E_FAILURE;
64 unsigned int i, to_count = 0, cc_count = 0, bcc_count = 0;
65 unsigned int to_size = 0, cc_size = 0, bcc_size = 0, subj_size, body_size;
67 char *to = NULL, *cc = NULL, *bcc = NULL;
68 const char *address, *subject, *body;
69 static const char format[] =
70 "mailto:\"%s\"?subject=\"%s\"&cc=\"%s\"&bcc=\"%s\"&body=\"%s\"";
71 char *mailto = NULL, *escape = NULL;
72 char empty_string[] = "";
73 HRESULT res;
74 DWORD size;
76 TRACE( "(0x%08x 0x%08lx %p 0x%08x 0x%08x)\n", session, uiparam,
77 message, flags, reserved );
79 /* Check to see if we have a Simple MAPI provider loaded */
80 if (mapiFunctions.MAPISendMail)
81 return mapiFunctions.MAPISendMail(session, uiparam, message, flags, reserved);
83 /* TODO: Check if we have an Extended MAPI provider, if so, implement
84 wrapper around that. */
86 /* Fall back on our own implementation */
87 if (!message) return MAPI_E_FAILURE;
89 for (i = 0; i < message->nRecipCount; i++)
91 if (!message->lpRecips)
93 WARN("No recipients found\n");
94 return MAPI_E_FAILURE;
97 address = message->lpRecips[i].lpszAddress;
98 if (address)
100 switch (message->lpRecips[i].ulRecipClass)
102 case MAPI_ORIG:
103 TRACE( "From: %s\n", debugstr_a(address) );
104 break;
105 case MAPI_TO:
106 TRACE( "To: %s\n", debugstr_a(address) );
107 to_size += lstrlenA( address ) + 1;
108 break;
109 case MAPI_CC:
110 TRACE( "Cc: %s\n", debugstr_a(address) );
111 cc_size += lstrlenA( address ) + 1;
112 break;
113 case MAPI_BCC:
114 TRACE( "Bcc: %s\n", debugstr_a(address) );
115 bcc_size += lstrlenA( address ) + 1;
116 break;
117 default:
118 TRACE( "Unknown recipient class: %d\n",
119 message->lpRecips[i].ulRecipClass );
122 else
123 FIXME("Name resolution and entry identifiers not supported\n");
125 if (message->nFileCount) FIXME("Ignoring attachments\n");
127 subject = message->lpszSubject ? message->lpszSubject : "";
128 body = message->lpszNoteText ? message->lpszNoteText : "";
130 TRACE( "Subject: %s\n", debugstr_a(subject) );
131 TRACE( "Body: %s\n", debugstr_a(body) );
133 subj_size = lstrlenA( subject );
134 body_size = lstrlenA( body );
136 ret = MAPI_E_INSUFFICIENT_MEMORY;
137 if (to_size)
139 to = HeapAlloc( GetProcessHeap(), 0, to_size );
140 if (!to) goto exit;
141 to[0] = 0;
143 if (cc_size)
145 cc = HeapAlloc( GetProcessHeap(), 0, cc_size );
146 if (!cc) goto exit;
147 cc[0] = 0;
149 if (bcc_size)
151 bcc = HeapAlloc( GetProcessHeap(), 0, bcc_size );
152 if (!bcc) goto exit;
153 bcc[0] = 0;
156 if (message->lpOriginator)
157 TRACE( "From: %s\n", debugstr_a(message->lpOriginator->lpszAddress) );
159 for (i = 0; i < message->nRecipCount; i++)
161 address = message->lpRecips[i].lpszAddress;
162 if (address)
164 switch (message->lpRecips[i].ulRecipClass)
166 case MAPI_TO:
167 if (to_count) lstrcatA( to, "," );
168 lstrcatA( to, address );
169 to_count++;
170 break;
171 case MAPI_CC:
172 if (cc_count) lstrcatA( cc, "," );
173 lstrcatA( cc, address );
174 cc_count++;
175 break;
176 case MAPI_BCC:
177 if (bcc_count) lstrcatA( bcc, "," );
178 lstrcatA( bcc, address );
179 bcc_count++;
180 break;
184 ret = MAPI_E_FAILURE;
185 size = sizeof(format) + to_size + cc_size + bcc_size + subj_size + body_size;
187 mailto = HeapAlloc( GetProcessHeap(), 0, size );
188 if (!mailto) goto exit;
190 sprintf( mailto, format, to ? to : "", subject, cc ? cc : "", bcc ? bcc : "", body );
192 size = 1;
193 res = UrlEscapeA( mailto, empty_string, &size, URL_ESCAPE_SPACES_ONLY );
194 if (res != E_POINTER) goto exit;
196 escape = HeapAlloc( GetProcessHeap(), 0, size );
197 if (!escape) goto exit;
199 res = UrlEscapeA( mailto, escape, &size, URL_ESCAPE_SPACES_ONLY );
200 if (res != S_OK) goto exit;
202 if ((UINT_PTR)ShellExecuteA( NULL, "open", escape, NULL, NULL, 0 ) > 32)
203 ret = SUCCESS_SUCCESS;
205 exit:
206 HeapFree( GetProcessHeap(), 0, to );
207 HeapFree( GetProcessHeap(), 0, cc );
208 HeapFree( GetProcessHeap(), 0, bcc );
209 HeapFree( GetProcessHeap(), 0, mailto );
210 HeapFree( GetProcessHeap(), 0, escape );
212 return ret;