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
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(winemapi
);
39 /* Escapes a string for use in mailto: URL */
40 static char *escape_string(char *in
, char *empty_string
)
50 res
= UrlEscapeA(in
, empty_string
, &size
, URL_ESCAPE_PERCENT
| URL_ESCAPE_SEGMENT_ONLY
);
54 escaped
= HeapAlloc(GetProcessHeap(), 0, size
);
59 /* If for some reason UrlEscape fails, just send the original text */
60 if (UrlEscapeA(in
, escaped
, &size
, URL_ESCAPE_PERCENT
| URL_ESCAPE_SEGMENT_ONLY
) != S_OK
)
62 HeapFree(GetProcessHeap(), 0, escaped
);
67 return escaped
? escaped
: empty_string
;
70 /**************************************************************************
73 * Send a message using a native mail client.
76 * session [I] Handle to a MAPI session.
77 * uiparam [I] Parent window handle.
78 * message [I] Pointer to a MAPIMessage structure.
80 * reserved [I] Reserved, pass 0.
83 * Success: SUCCESS_SUCCESS
84 * Failure: MAPI_E_FAILURE
87 ULONG WINAPI
MAPISendMail(LHANDLE session
, ULONG_PTR uiparam
,
88 lpMapiMessage message
, FLAGS flags
, ULONG reserved
)
90 ULONG ret
= MAPI_E_FAILURE
;
91 unsigned int i
, to_count
= 0, cc_count
= 0, bcc_count
= 0;
92 unsigned int to_size
= 0, cc_size
= 0, bcc_size
= 0, subj_size
, body_size
;
94 char *to
= NULL
, *cc
= NULL
, *bcc
= NULL
, *subject
= NULL
, *body
= NULL
;
96 static const char format
[] =
97 "mailto:\"%s\"?subject=\"%s\"&cc=\"%s\"&bcc=\"%s\"&body=\"%s\"";
98 static const char smtp
[] = "smtp:";
99 char *mailto
= NULL
, *escape
= NULL
;
100 char empty_string
[] = "";
104 TRACE("(0x%08lx 0x%08lx %p 0x%08x 0x%08x)\n", session
, uiparam
,
105 message
, flags
, reserved
);
108 return MAPI_E_FAILURE
;
110 for (i
= 0; i
< message
->nRecipCount
; i
++)
112 if (!message
->lpRecips
)
114 WARN("No recipients found\n");
115 return MAPI_E_FAILURE
;
118 address
= message
->lpRecips
[i
].lpszAddress
;
122 if (!_strnicmp(address
, smtp
, sizeof(smtp
) - 1))
123 address
+= sizeof(smtp
) - 1;
125 switch (message
->lpRecips
[i
].ulRecipClass
)
128 TRACE("From: %s\n", debugstr_a(address
));
132 TRACE("To: %s\n", debugstr_a(address
));
133 to_size
+= lstrlenA(address
) + 1;
137 TRACE("Cc: %s\n", debugstr_a(address
));
138 cc_size
+= lstrlenA(address
) + 1;
142 TRACE("Bcc: %s\n", debugstr_a(address
));
143 bcc_size
+= lstrlenA(address
) + 1;
147 TRACE("Unknown recipient class: %d\n",
148 message
->lpRecips
[i
].ulRecipClass
);
152 FIXME("Name resolution and entry identifiers not supported\n");
155 if (message
->nFileCount
)
157 FIXME("Ignoring %u attachments:\n", message
->nFileCount
);
158 for (i
= 0; i
< message
->nFileCount
; i
++)
159 FIXME("\t%s (%s)\n", debugstr_a(message
->lpFiles
[i
].lpszPathName
),
160 debugstr_a(message
->lpFiles
[i
].lpszFileName
));
163 /* Escape subject and body */
164 subject
= escape_string(message
->lpszSubject
, empty_string
);
165 body
= escape_string(message
->lpszNoteText
, empty_string
);
167 TRACE("Subject: %s\n", debugstr_a(subject
));
168 TRACE("Body: %s\n", debugstr_a(body
));
170 subj_size
= lstrlenA(subject
);
171 body_size
= lstrlenA(body
);
173 ret
= MAPI_E_INSUFFICIENT_MEMORY
;
177 to
= HeapAlloc(GetProcessHeap(), 0, to_size
);
187 cc
= HeapAlloc(GetProcessHeap(), 0, cc_size
);
197 bcc
= HeapAlloc(GetProcessHeap(), 0, bcc_size
);
205 if (message
->lpOriginator
)
206 TRACE("From: %s\n", debugstr_a(message
->lpOriginator
->lpszAddress
));
208 for (i
= 0; i
< message
->nRecipCount
; i
++)
210 address
= message
->lpRecips
[i
].lpszAddress
;
214 if (!_strnicmp(address
, smtp
, sizeof(smtp
) - 1))
215 address
+= sizeof(smtp
) - 1;
217 switch (message
->lpRecips
[i
].ulRecipClass
)
223 lstrcatA(to
, address
);
231 lstrcatA(cc
, address
);
239 lstrcatA(bcc
, address
);
245 ret
= MAPI_E_FAILURE
;
246 size
= sizeof(format
) + to_size
+ cc_size
+ bcc_size
+ subj_size
+ body_size
;
248 mailto
= HeapAlloc(GetProcessHeap(), 0, size
);
253 sprintf(mailto
, format
, to
? to
: "", subject
, cc
? cc
: "", bcc
? bcc
: "", body
);
256 res
= UrlEscapeA(mailto
, empty_string
, &size
, URL_ESCAPE_SPACES_ONLY
);
258 if (res
!= E_POINTER
)
261 escape
= HeapAlloc(GetProcessHeap(), 0, size
);
266 res
= UrlEscapeA(mailto
, escape
, &size
, URL_ESCAPE_SPACES_ONLY
);
271 TRACE("Executing winebrowser.exe with parameters '%s'\n", debugstr_a(escape
));
273 if ((UINT_PTR
) ShellExecuteA(NULL
, "open", "winebrowser.exe", escape
, NULL
, 0) > 32)
274 ret
= SUCCESS_SUCCESS
;
277 HeapFree(GetProcessHeap(), 0, to
);
278 HeapFree(GetProcessHeap(), 0, cc
);
279 HeapFree(GetProcessHeap(), 0, bcc
);
280 HeapFree(GetProcessHeap(), 0, mailto
);
281 HeapFree(GetProcessHeap(), 0, escape
);
283 if (subject
!= empty_string
)
284 HeapFree(GetProcessHeap(), 0, subject
);
286 if (body
!= empty_string
)
287 HeapFree(GetProcessHeap(), 0, body
);
292 ULONG WINAPI
MAPISendDocuments(ULONG_PTR uiparam
, LPSTR delim
, LPSTR paths
,
293 LPSTR filenames
, ULONG reserved
)
295 return MAPI_E_NOT_SUPPORTED
;