2
Copyright (c) 2007 Ian Cook and John Popplewell
4
Permission is hereby granted, free of charge, to any person obtaining
5
a copy of this software and associated documentation files (the
6
"Software"), to deal in the Software without restriction, including
7
without limitation the rights to use, copy, modify, merge, publish,
8
distribute, sublicense, and/or sell copies of the Software, and to
9
permit persons to whom the Software is furnished to do so, subject to
10
the following conditions:
12
The above copyright notice and this permission notice shall be included
13
in all copies or substantial portions of the Software.
15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
Contact : John Popplewell
26
Email : john@johnnypops.demon.co.uk
27
Web : http://www.johnnypops.demon.co.uk/python/
28
Origin : Based on the original script by Ian Cook
29
http://www.kirbyfooty.com/simplemapi.py
30
Comments: Works (and tested) with:
31
Outlook Express, Outlook 97 and 2000,
32
Eudora, Incredimail and Mozilla Thunderbird (1.5.0.2)
33
Thanks : Werner F. Bruhin and Michele Petrazzo on the ctypes list.
35
If you have any bug-fixes, enhancements or suggestions regarding this
36
software, please contact me at the above email address.
39
from __future__ import absolute_import
46
LPLHANDLE = POINTER(LHANDLE)
51
MAPI_E_USER_ABORT = MAPI_USER_ABORT
53
MAPI_E_LOGON_FAILURE = 3
54
MAPI_E_LOGIN_FAILURE = MAPI_E_LOGON_FAILURE
56
MAPI_E_INSUFFICIENT_MEMORY = 5
57
MAPI_E_ACCESS_DENIED = 6
58
MAPI_E_TOO_MANY_SESSIONS = 8
59
MAPI_E_TOO_MANY_FILES = 9
60
MAPI_E_TOO_MANY_RECIPIENTS = 10
61
MAPI_E_ATTACHMENT_NOT_FOUND = 11
62
MAPI_E_ATTACHMENT_OPEN_FAILURE = 12
63
MAPI_E_ATTACHMENT_WRITE_FAILURE = 13
64
MAPI_E_UNKNOWN_RECIPIENT = 14
65
MAPI_E_BAD_RECIPTYPE = 15
66
MAPI_E_NO_MESSAGES = 16
67
MAPI_E_INVALID_MESSAGE = 17
68
MAPI_E_TEXT_TOO_LARGE = 18
69
MAPI_E_INVALID_SESSION = 19
70
MAPI_E_TYPE_NOT_SUPPORTED = 20
71
MAPI_E_AMBIGUOUS_RECIPIENT = 21
72
MAPI_E_AMBIG_RECIP = MAPI_E_AMBIGUOUS_RECIPIENT
73
MAPI_E_MESSAGE_IN_USE = 22
74
MAPI_E_NETWORK_FAILURE = 23
75
MAPI_E_INVALID_EDITFIELDS = 24
76
MAPI_E_INVALID_RECIPS = 25
77
MAPI_E_NOT_SUPPORTED = 26
86
class MapiRecipDesc(Structure):
88
('ulReserved', c_ulong),
89
('ulRecipClass', c_ulong),
90
('lpszName', c_char_p),
91
('lpszAddress', c_char_p),
92
('ulEIDSize', c_ulong),
93
('lpEntryID', c_void_p),
97
lpMapiRecipDesc = POINTER(MapiRecipDesc)
98
lppMapiRecipDesc = POINTER(lpMapiRecipDesc)
101
class MapiFileDesc(Structure):
103
('ulReserved', c_ulong),
104
('flFlags', c_ulong),
105
('nPosition', c_ulong),
106
('lpszPathName', c_char_p),
107
('lpszFileName', c_char_p),
108
('lpFileType', c_void_p),
112
lpMapiFileDesc = POINTER(MapiFileDesc)
115
class MapiMessage(Structure):
117
('ulReserved', c_ulong),
118
('lpszSubject', c_char_p),
119
('lpszNoteText', c_char_p),
120
('lpszMessageType', c_char_p),
121
('lpszDateReceived', c_char_p),
122
('lpszConversationID', c_char_p),
124
('lpOriginator', lpMapiRecipDesc),
125
('nRecipCount', c_ulong),
126
('lpRecips', lpMapiRecipDesc),
127
('nFileCount', c_ulong),
128
('lpFiles', lpMapiFileDesc),
132
lpMapiMessage = POINTER(MapiMessage)
135
MAPISendMail = MAPI.MAPISendMail
136
MAPISendMail.restype = c_ulong
137
MAPISendMail.argtypes = (LHANDLE, c_ulong, lpMapiMessage, FLAGS, c_ulong)
139
MAPIResolveName = MAPI.MAPIResolveName
140
MAPIResolveName.restype = c_ulong
141
MAPIResolveName.argtypes = (
142
LHANDLE, c_ulong, c_char_p, FLAGS, c_ulong, lppMapiRecipDesc)
144
MAPIFreeBuffer = MAPI.MAPIFreeBuffer
145
MAPIFreeBuffer.restype = c_ulong
146
MAPIFreeBuffer.argtypes = (c_void_p, )
148
MAPILogon = MAPI.MAPILogon
149
MAPILogon.restype = c_ulong
150
MAPILogon.argtypes = (LHANDLE, c_char_p, c_char_p, FLAGS, c_ulong, LPLHANDLE)
152
MAPILogoff = MAPI.MAPILogoff
153
MAPILogoff.restype = c_ulong
154
MAPILogoff.argtypes = (LHANDLE, c_ulong, FLAGS, c_ulong)
157
class MAPIError(WindowsError):
159
def __init__(self, code):
160
WindowsError.__init__(self)
164
return 'MAPI error %d' % (self.code,)
167
def _logon(profileName=None, password=None):
169
rc = MAPILogon(0, profileName, password, MAPI_LOGON_UI, 0, byref(pSession))
170
if rc != SUCCESS_SUCCESS:
175
def _logoff(session):
176
rc = MAPILogoff(session, 0, 0, 0)
177
if rc != SUCCESS_SUCCESS:
181
def _resolveName(session, name):
182
pRecipDesc = lpMapiRecipDesc()
183
rc = MAPIResolveName(session, 0, name, 0, 0, byref(pRecipDesc))
184
if rc != SUCCESS_SUCCESS:
186
rd = pRecipDesc.contents
187
name, address = rd.lpszName, rd.lpszAddress
188
rc = MAPIFreeBuffer(pRecipDesc)
189
if rc != SUCCESS_SUCCESS:
194
def _sendMail(session, recipient, subject, body, attach):
195
nFileCount = len(attach)
197
MapiFileDesc_A = MapiFileDesc * len(attach)
198
fda = MapiFileDesc_A()
199
for fd, fa in zip(fda, attach):
204
fd.lpszFileName = None
208
lpFiles = lpMapiFileDesc()
210
RecipWork = recipient.split(';')
211
RecipCnt = len(RecipWork)
212
MapiRecipDesc_A = MapiRecipDesc * len(RecipWork)
213
rda = MapiRecipDesc_A()
214
for rd, ra in zip(rda, RecipWork):
216
rd.ulRecipClass = MAPI_TO
218
rd.lpszName, rd.lpszAddress = _resolveName(session, ra)
220
# work-round for Mozilla Thunderbird
221
rd.lpszName, rd.lpszAddress = None, ra
226
msg = MapiMessage(0, subject, body, None, None, None, 0, lpMapiRecipDesc(),
230
rc = MAPISendMail(session, 0, byref(msg), MAPI_DIALOG, 0)
231
if rc != SUCCESS_SUCCESS:
235
def SendMail(recipient, subject="", body="", attachfiles=""):
236
"""Post an e-mail message using Simple MAPI
238
recipient - string: address to send to (multiple addresses separated with a semicolon)
239
subject - string: subject header
240
body - string: message text
241
attach - string: files to attach (multiple attachments separated with a semicolon)
245
AttachWork = attachfiles.split(';')
247
if os.path.exists(f):
248
attach.append(os.path.abspath(f))
250
restore = os.getcwd()
254
_sendMail(session, recipient, subject, body, attach)
261
if __name__ == '__main__':
263
recipient = "test@johnnypops.demon.co.uk"
264
subject = "Test Message Subject"
265
body = "Hi,\r\n\r\nthis is a quick test message,\r\n\r\ncheers,\r\nJohn."
266
attachment = sys.argv[0]
267
SendMail(recipient, subject, body, attachment)