mapi: Add some missing MAPI prototypes.
[wine] / dlls / mapi32 / sendmail.c
1 /*
2  * MAPISendMail implementation
3  *
4  * Copyright 2005 Hans Leidekker
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdio.h>
25 #include <stdarg.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
30 #include "winnls.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
38 WINE_DEFAULT_DEBUG_CHANNEL(mapi);
39
40 /**************************************************************************
41  *  MAPISendMail        (MAPI32.211)
42  *
43  * Send a mail.
44  *
45  * PARAMS
46  *  session  [I] Handle to a MAPI session.
47  *  uiparam  [I] Parent window handle.
48  *  message  [I] Pointer to a MAPIMessage structure.
49  *  flags    [I] Flags.
50  *  reserved [I] Reserved, pass 0.
51  *
52  * RETURNS
53  *  Success: SUCCESS_SUCCESS
54  *  Failure: MAPI_E_FAILURE
55  *
56  * NOTES
57  *  This is a temporary hack. 
58  */
59 ULONG WINAPI MAPISendMail( LHANDLE session, ULONG_PTR uiparam,
60     lpMapiMessage message, FLAGS flags, ULONG reserved )
61 {
62     ULONG ret = MAPI_E_FAILURE;
63     unsigned int i, to_count = 0, cc_count = 0, bcc_count = 0;
64     unsigned int to_size = 0, cc_size = 0, bcc_size = 0, subj_size, body_size;
65
66     char *address = "", *to = NULL, *cc = NULL, *bcc = NULL, *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     HRESULT res;
71     DWORD size;
72
73     TRACE( "(0x%08lx 0x%08lx %p 0x%08lx 0x%08lx)\n", session, uiparam,
74            message, flags, reserved );
75
76     if (!message) return MAPI_E_FAILURE;
77
78     for (i = 0; i < message->nRecipCount; i++)
79     {
80         if (!message->lpRecips)
81         {
82             WARN("No recipients found\n");
83             return MAPI_E_FAILURE;
84         }
85
86         address = message->lpRecips[i].lpszAddress;
87         if (address)
88         {
89             switch (message->lpRecips[i].ulRecipClass)
90             {
91             case MAPI_ORIG:
92                 TRACE( "From: %s", debugstr_a(address) );
93                 break;
94             case MAPI_TO:
95                 TRACE( "To: %s", debugstr_a(address) );
96                 to_size += lstrlenA( address ) + 1;
97                 break;
98             case MAPI_CC:
99                 TRACE( "Cc: %s", debugstr_a(address) );
100                 cc_size += lstrlenA( address ) + 1;
101                 break;
102             case MAPI_BCC:
103                 TRACE( "Bcc: %s", debugstr_a(address) );
104                 bcc_size += lstrlenA( address ) + 1;
105                 break;
106             default:
107                 TRACE( "Unknown recipient class: %ld\n",
108                        message->lpRecips[i].ulRecipClass );
109             }
110         }
111         else
112             FIXME("Name resolution and entry identifiers not supported\n");
113     }
114     if (message->nFileCount) FIXME("Ignoring attachments\n");
115
116     subject = message->lpszSubject ? message->lpszSubject : "";
117     body = message->lpszNoteText ? message->lpszNoteText : "";
118
119     TRACE( "Subject: %s\n", debugstr_a(subject) );
120     TRACE( "Body: %s\n", debugstr_a(body) );
121
122     subj_size = lstrlenA( subject );
123     body_size = lstrlenA( body );
124
125     ret = MAPI_E_INSUFFICIENT_MEMORY;
126     if (to_size)
127     {
128         to = HeapAlloc( GetProcessHeap(), 0, to_size );
129         if (!to) goto exit;
130     }
131     if (cc_size)
132     {
133         cc = HeapAlloc( GetProcessHeap(), 0, cc_size );
134         if (!cc) goto exit;
135     }
136     if (bcc_size)
137     {
138         bcc = HeapAlloc( GetProcessHeap(), 0, bcc_size );
139         if (!bcc) goto exit;
140     }
141
142     if (message->lpOriginator)
143         TRACE( "From: %s\n", debugstr_a(message->lpOriginator->lpszAddress) );
144
145     for (i = 0; i < message->nRecipCount; i++)
146     {
147         address = message->lpRecips[i].lpszAddress;
148         if (address)
149         {
150             switch (message->lpRecips[i].ulRecipClass)
151             {
152             case MAPI_TO:
153                 if (to_count) lstrcatA( to, "," );
154                 lstrcatA( to, address );
155                 to_count++;
156                 break;
157             case MAPI_CC:
158                 if (cc_count) lstrcatA( cc, "," );
159                 lstrcatA( cc, address );
160                 cc_count++;
161                 break;
162             case MAPI_BCC:
163                 if (bcc_count) lstrcatA( bcc, "," );
164                 lstrcatA( bcc, address );
165                 bcc_count++;
166                 break;
167             }
168         }
169     }
170     ret = MAPI_E_FAILURE;
171     size = sizeof(format) + to_size + cc_size + bcc_size + subj_size + body_size;
172     
173     mailto = HeapAlloc( GetProcessHeap(), 0, size );
174     if (!mailto) goto exit;
175
176     sprintf( mailto, format, to ? to : "", subject, cc ? cc : "", bcc ? bcc : "", body );
177
178     size = 0;
179     res = UrlEscapeA( mailto, NULL, &size, URL_ESCAPE_SPACES_ONLY );
180     if (res != E_POINTER) goto exit;
181
182     escape = HeapAlloc( GetProcessHeap(), 0, size );
183     if (!escape) goto exit;
184
185     res = UrlEscapeA( mailto, escape, &size, URL_ESCAPE_SPACES_ONLY );
186     if (res != S_OK) goto exit;
187
188     if ((UINT_PTR)ShellExecuteA( NULL, "open", escape, NULL, NULL, 0 ) > 32)
189         ret = SUCCESS_SUCCESS;
190
191 exit:
192     HeapFree( GetProcessHeap(), 0, to );
193     HeapFree( GetProcessHeap(), 0, cc );
194     HeapFree( GetProcessHeap(), 0, bcc );
195     HeapFree( GetProcessHeap(), 0, mailto );
196     HeapFree( GetProcessHeap(), 0, escape );
197
198     return ret;
199 }