2
Based on this original script: http://www.kirbyfooty.com/simplemapi.py
4
Now works (and tested) with:
5
Outlook Express, Outlook 97 and 2000,
6
Eudora, Incredimail and Mozilla Thunderbird (1.5.0.2)
12
john@johnnypops.demon.co.uk
13
http://www.johnnypops.demon.co.uk/python/
15
Thanks to Werner F. Bruhin and Michele Petrazzo on the ctypes list.
23
LPLHANDLE = POINTER(LHANDLE)
35
class MapiRecipDesc(Structure):
37
('ulReserved', c_ulong),
38
('ulRecipClass', c_ulong),
39
('lpszName', c_char_p),
40
('lpszAddress', c_char_p),
41
('ulEIDSize', c_ulong),
42
('lpEntryID', c_void_p),
44
lpMapiRecipDesc = POINTER(MapiRecipDesc)
45
lppMapiRecipDesc = POINTER(lpMapiRecipDesc)
47
class MapiFileDesc(Structure):
49
('ulReserved', c_ulong),
51
('nPosition', c_ulong),
52
('lpszPathName', c_char_p),
53
('lpszFileName', c_char_p),
54
('lpFileType', c_void_p),
56
lpMapiFileDesc = POINTER(MapiFileDesc)
58
class MapiMessage(Structure):
60
('ulReserved', c_ulong),
61
('lpszSubject', c_char_p),
62
('lpszNoteText', c_char_p),
63
('lpszMessageType', c_char_p),
64
('lpszDateReceived', c_char_p),
65
('lpszConversationID', c_char_p),
67
('lpOriginator', lpMapiRecipDesc),
68
('nRecipCount', c_ulong),
69
('lpRecips', lpMapiRecipDesc),
70
('nFileCount', c_ulong),
71
('lpFiles', lpMapiFileDesc),
73
lpMapiMessage = POINTER(MapiMessage)
76
MAPISendMail = MAPI.MAPISendMail
77
MAPISendMail.restype = c_ulong
78
MAPISendMail.argtypes = (LHANDLE, c_ulong, lpMapiMessage, FLAGS, c_ulong)
80
MAPIResolveName = MAPI.MAPIResolveName
81
MAPIResolveName.restype = c_ulong
82
MAPIResolveName.argtypes= (LHANDLE, c_ulong, c_char_p, FLAGS, c_ulong, lppMapiRecipDesc)
84
MAPIFreeBuffer = MAPI.MAPIFreeBuffer
85
MAPIFreeBuffer.restype = c_ulong
86
MAPIFreeBuffer.argtypes = (c_void_p, )
88
MAPILogon = MAPI.MAPILogon
89
MAPILogon.restype = c_ulong
90
MAPILogon.argtypes = (LHANDLE, c_char_p, c_char_p, FLAGS, c_ulong, LPLHANDLE)
92
MAPILogoff = MAPI.MAPILogoff
93
MAPILogoff.restype = c_ulong
94
MAPILogoff.argtypes = (LHANDLE, c_ulong, FLAGS, c_ulong)
97
def _logon(profileName=None, password=None):
99
rc = MAPILogon(0, profileName, password, MAPI_LOGON_UI, 0, byref(pSession))
100
if rc != SUCCESS_SUCCESS:
101
raise WindowsError, "MAPI error %i" % rc
105
def _logoff(session):
106
rc = MAPILogoff(session, 0, 0, 0)
107
if rc != SUCCESS_SUCCESS:
108
raise WindowsError, "MAPI error %i" % rc
111
def _resolveName(session, name):
112
pRecipDesc = lpMapiRecipDesc()
113
rc = MAPIResolveName(session, 0, name, 0, 0, byref(pRecipDesc))
114
if rc != SUCCESS_SUCCESS:
115
raise WindowsError, "MAPI error %i" % rc
116
rd = pRecipDesc.contents
117
name, address = rd.lpszName, rd.lpszAddress
118
rc = MAPIFreeBuffer(pRecipDesc)
119
if rc != SUCCESS_SUCCESS:
120
raise WindowsError, "MAPI error %i" % rc
124
def _sendMail(session, recipient, subject, body, attach):
125
nFileCount = len(attach)
127
MapiFileDesc_A = MapiFileDesc * len(attach)
128
fda = MapiFileDesc_A()
129
for fd, fa in zip(fda, attach):
134
fd.lpszFileName = None
138
lpFiles = lpMapiFileDesc()
140
RecipWork = recipient.split(';')
141
RecipCnt = len(RecipWork)
142
MapiRecipDesc_A = MapiRecipDesc * len(RecipWork)
143
rda = MapiRecipDesc_A()
144
for rd, ra in zip(rda, RecipWork):
146
rd.ulRecipClass = MAPI_TO
148
rd.lpszName, rd.lpszAddress = _resolveName(session, ra)
150
# work-round for Mozilla Thunderbird
151
rd.lpszName, rd.lpszAddress = None, ra
156
msg = MapiMessage(0, subject, body, None, None, None, 0, lpMapiRecipDesc(),
160
rc = MAPISendMail(session, 0, byref(msg), MAPI_DIALOG, 0)
161
if rc != SUCCESS_SUCCESS and rc != MAPI_USER_ABORT:
162
raise WindowsError, "MAPI error %i" % rc
165
def SendMail(recipient, subject="", body="", attachfiles=""):
166
"""Post an e-mail message using Simple MAPI
168
recipient - string: address to send to (multiple addresses separated with a semicolon)
169
subject - string: subject header
170
body - string: message text
171
attach - string: files to attach (multiple attachments separated with a semicolon)
175
AttachWork = attachfiles.split(';')
176
for file in AttachWork:
177
if os.path.exists(file):
179
attach = map(os.path.abspath, attach)
181
restore = os.getcwd()
185
_sendMail(session, recipient, subject, body, attach)
192
if __name__ == '__main__':
194
recipient = "test@johnnypops.demon.co.uk"
195
subject = "Test Message Subject"
196
body = "Hi,\r\n\r\nthis is a quick test message,\r\n\r\ncheers,\r\nJohn."
197
attachment = sys.argv[0]
198
SendMail(recipient, subject, body, attachment)