2681.3.3
by Lukáš Lalinsky
Convert to UNIX line endings. |
1 |
"""
|
2681.3.6
by Lukáš Lalinsky
New version of simplemapi.py with MIT license. |
2 |
Copyright (c) 2007 Ian Cook and John Popplewell
|
3 |
||
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:
|
|
11 |
||
12 |
The above copyright notice and this permission notice shall be included
|
|
13 |
in all copies or substantial portions of the Software.
|
|
14 |
||
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.
|
|
22 |
||
23 |
Date : 11 August 2007
|
|
24 |
Version : 1.0.1
|
|
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:
|
|
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
31 |
Outlook Express, Outlook 97 and 2000,
|
2681.3.6
by Lukáš Lalinsky
New version of simplemapi.py with MIT license. |
32 |
Eudora, Incredimail and Mozilla Thunderbird (1.5.0.2)
|
33 |
Thanks : Werner F. Bruhin and Michele Petrazzo on the ctypes list.
|
|
34 |
||
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
35 |
If you have any bug-fixes, enhancements or suggestions regarding this
|
2681.3.6
by Lukáš Lalinsky
New version of simplemapi.py with MIT license. |
36 |
software, please contact me at the above email address.
|
2681.3.3
by Lukáš Lalinsky
Convert to UNIX line endings. |
37 |
"""
|
38 |
||
6379.6.7
by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear. |
39 |
from __future__ import absolute_import |
40 |
||
2681.3.3
by Lukáš Lalinsky
Convert to UNIX line endings. |
41 |
import os |
42 |
from ctypes import * |
|
43 |
||
44 |
FLAGS = c_ulong |
|
45 |
LHANDLE = c_ulong |
|
46 |
LPLHANDLE = POINTER(LHANDLE) |
|
47 |
||
48 |
# Return codes
|
|
2681.3.6
by Lukáš Lalinsky
New version of simplemapi.py with MIT license. |
49 |
SUCCESS_SUCCESS = 0 |
50 |
MAPI_USER_ABORT = 1 |
|
51 |
MAPI_E_USER_ABORT = MAPI_USER_ABORT |
|
52 |
MAPI_E_FAILURE = 2 |
|
53 |
MAPI_E_LOGON_FAILURE = 3 |
|
54 |
MAPI_E_LOGIN_FAILURE = MAPI_E_LOGON_FAILURE |
|
55 |
MAPI_E_DISK_FULL = 4 |
|
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 |
|
2681.3.3
by Lukáš Lalinsky
Convert to UNIX line endings. |
78 |
# Recipient class
|
79 |
MAPI_ORIG = 0 |
|
80 |
MAPI_TO = 1 |
|
81 |
# Send flags
|
|
82 |
MAPI_LOGON_UI = 1 |
|
83 |
MAPI_DIALOG = 8 |
|
84 |
||
85 |
class MapiRecipDesc(Structure): |
|
86 |
_fields_ = [ |
|
87 |
('ulReserved', c_ulong), |
|
88 |
('ulRecipClass', c_ulong), |
|
89 |
('lpszName', c_char_p), |
|
90 |
('lpszAddress', c_char_p), |
|
91 |
('ulEIDSize', c_ulong), |
|
92 |
('lpEntryID', c_void_p), |
|
93 |
]
|
|
94 |
lpMapiRecipDesc = POINTER(MapiRecipDesc) |
|
95 |
lppMapiRecipDesc = POINTER(lpMapiRecipDesc) |
|
96 |
||
97 |
class MapiFileDesc(Structure): |
|
98 |
_fields_ = [ |
|
99 |
('ulReserved', c_ulong), |
|
100 |
('flFlags', c_ulong), |
|
101 |
('nPosition', c_ulong), |
|
102 |
('lpszPathName', c_char_p), |
|
103 |
('lpszFileName', c_char_p), |
|
104 |
('lpFileType', c_void_p), |
|
105 |
]
|
|
106 |
lpMapiFileDesc = POINTER(MapiFileDesc) |
|
107 |
||
108 |
class MapiMessage(Structure): |
|
109 |
_fields_ = [ |
|
110 |
('ulReserved', c_ulong), |
|
111 |
('lpszSubject', c_char_p), |
|
112 |
('lpszNoteText', c_char_p), |
|
113 |
('lpszMessageType', c_char_p), |
|
114 |
('lpszDateReceived', c_char_p), |
|
115 |
('lpszConversationID', c_char_p), |
|
116 |
('flFlags', FLAGS), |
|
117 |
('lpOriginator', lpMapiRecipDesc), |
|
118 |
('nRecipCount', c_ulong), |
|
119 |
('lpRecips', lpMapiRecipDesc), |
|
120 |
('nFileCount', c_ulong), |
|
121 |
('lpFiles', lpMapiFileDesc), |
|
122 |
]
|
|
123 |
lpMapiMessage = POINTER(MapiMessage) |
|
124 |
||
125 |
MAPI = windll.mapi32 |
|
126 |
MAPISendMail = MAPI.MAPISendMail |
|
127 |
MAPISendMail.restype = c_ulong |
|
128 |
MAPISendMail.argtypes = (LHANDLE, c_ulong, lpMapiMessage, FLAGS, c_ulong) |
|
129 |
||
130 |
MAPIResolveName = MAPI.MAPIResolveName |
|
131 |
MAPIResolveName.restype = c_ulong |
|
132 |
MAPIResolveName.argtypes= (LHANDLE, c_ulong, c_char_p, FLAGS, c_ulong, lppMapiRecipDesc) |
|
133 |
||
134 |
MAPIFreeBuffer = MAPI.MAPIFreeBuffer |
|
135 |
MAPIFreeBuffer.restype = c_ulong |
|
136 |
MAPIFreeBuffer.argtypes = (c_void_p, ) |
|
137 |
||
138 |
MAPILogon = MAPI.MAPILogon |
|
139 |
MAPILogon.restype = c_ulong |
|
140 |
MAPILogon.argtypes = (LHANDLE, c_char_p, c_char_p, FLAGS, c_ulong, LPLHANDLE) |
|
141 |
||
142 |
MAPILogoff = MAPI.MAPILogoff |
|
143 |
MAPILogoff.restype = c_ulong |
|
144 |
MAPILogoff.argtypes = (LHANDLE, c_ulong, FLAGS, c_ulong) |
|
145 |
||
146 |
||
2681.3.6
by Lukáš Lalinsky
New version of simplemapi.py with MIT license. |
147 |
class MAPIError(WindowsError): |
148 |
||
149 |
def __init__(self, code): |
|
150 |
WindowsError.__init__(self) |
|
151 |
self.code = code |
|
152 |
||
153 |
def __str__(self): |
|
154 |
return 'MAPI error %d' % (self.code,) |
|
155 |
||
156 |
||
2681.3.3
by Lukáš Lalinsky
Convert to UNIX line endings. |
157 |
def _logon(profileName=None, password=None): |
158 |
pSession = LHANDLE() |
|
159 |
rc = MAPILogon(0, profileName, password, MAPI_LOGON_UI, 0, byref(pSession)) |
|
160 |
if rc != SUCCESS_SUCCESS: |
|
2681.3.6
by Lukáš Lalinsky
New version of simplemapi.py with MIT license. |
161 |
raise MAPIError, rc |
2681.3.3
by Lukáš Lalinsky
Convert to UNIX line endings. |
162 |
return pSession |
163 |
||
164 |
||
165 |
def _logoff(session): |
|
166 |
rc = MAPILogoff(session, 0, 0, 0) |
|
167 |
if rc != SUCCESS_SUCCESS: |
|
2681.3.6
by Lukáš Lalinsky
New version of simplemapi.py with MIT license. |
168 |
raise MAPIError, rc |
2681.3.3
by Lukáš Lalinsky
Convert to UNIX line endings. |
169 |
|
170 |
||
171 |
def _resolveName(session, name): |
|
172 |
pRecipDesc = lpMapiRecipDesc() |
|
173 |
rc = MAPIResolveName(session, 0, name, 0, 0, byref(pRecipDesc)) |
|
174 |
if rc != SUCCESS_SUCCESS: |
|
2681.3.6
by Lukáš Lalinsky
New version of simplemapi.py with MIT license. |
175 |
raise MAPIError, rc |
2681.3.3
by Lukáš Lalinsky
Convert to UNIX line endings. |
176 |
rd = pRecipDesc.contents |
177 |
name, address = rd.lpszName, rd.lpszAddress |
|
178 |
rc = MAPIFreeBuffer(pRecipDesc) |
|
179 |
if rc != SUCCESS_SUCCESS: |
|
2681.3.6
by Lukáš Lalinsky
New version of simplemapi.py with MIT license. |
180 |
raise MAPIError, rc |
2681.3.3
by Lukáš Lalinsky
Convert to UNIX line endings. |
181 |
return name, address |
182 |
||
183 |
||
184 |
def _sendMail(session, recipient, subject, body, attach): |
|
185 |
nFileCount = len(attach) |
|
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
186 |
if attach: |
187 |
MapiFileDesc_A = MapiFileDesc * len(attach) |
|
188 |
fda = MapiFileDesc_A() |
|
189 |
for fd, fa in zip(fda, attach): |
|
190 |
fd.ulReserved = 0 |
|
191 |
fd.flFlags = 0 |
|
192 |
fd.nPosition = -1 |
|
193 |
fd.lpszPathName = fa |
|
194 |
fd.lpszFileName = None |
|
195 |
fd.lpFileType = None |
|
2681.3.3
by Lukáš Lalinsky
Convert to UNIX line endings. |
196 |
lpFiles = fda |
197 |
else: |
|
198 |
lpFiles = lpMapiFileDesc() |
|
199 |
||
200 |
RecipWork = recipient.split(';') |
|
201 |
RecipCnt = len(RecipWork) |
|
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
202 |
MapiRecipDesc_A = MapiRecipDesc * len(RecipWork) |
203 |
rda = MapiRecipDesc_A() |
|
2681.3.3
by Lukáš Lalinsky
Convert to UNIX line endings. |
204 |
for rd, ra in zip(rda, RecipWork): |
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
205 |
rd.ulReserved = 0 |
2681.3.3
by Lukáš Lalinsky
Convert to UNIX line endings. |
206 |
rd.ulRecipClass = MAPI_TO |
207 |
try: |
|
208 |
rd.lpszName, rd.lpszAddress = _resolveName(session, ra) |
|
209 |
except WindowsError: |
|
210 |
# work-round for Mozilla Thunderbird
|
|
211 |
rd.lpszName, rd.lpszAddress = None, ra |
|
212 |
rd.ulEIDSize = 0 |
|
213 |
rd.lpEntryID = None |
|
214 |
recip = rda |
|
215 |
||
216 |
msg = MapiMessage(0, subject, body, None, None, None, 0, lpMapiRecipDesc(), |
|
217 |
RecipCnt, recip, |
|
218 |
nFileCount, lpFiles) |
|
219 |
||
220 |
rc = MAPISendMail(session, 0, byref(msg), MAPI_DIALOG, 0) |
|
2681.3.6
by Lukáš Lalinsky
New version of simplemapi.py with MIT license. |
221 |
if rc != SUCCESS_SUCCESS: |
222 |
raise MAPIError, rc |
|
2681.3.3
by Lukáš Lalinsky
Convert to UNIX line endings. |
223 |
|
224 |
||
225 |
def SendMail(recipient, subject="", body="", attachfiles=""): |
|
226 |
"""Post an e-mail message using Simple MAPI
|
|
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
227 |
|
2681.3.3
by Lukáš Lalinsky
Convert to UNIX line endings. |
228 |
recipient - string: address to send to (multiple addresses separated with a semicolon)
|
229 |
subject - string: subject header
|
|
230 |
body - string: message text
|
|
231 |
attach - string: files to attach (multiple attachments separated with a semicolon)
|
|
232 |
"""
|
|
233 |
||
234 |
attach = [] |
|
235 |
AttachWork = attachfiles.split(';') |
|
236 |
for file in AttachWork: |
|
237 |
if os.path.exists(file): |
|
238 |
attach.append(file) |
|
239 |
attach = map(os.path.abspath, attach) |
|
240 |
||
241 |
restore = os.getcwd() |
|
242 |
try: |
|
243 |
session = _logon() |
|
244 |
try: |
|
245 |
_sendMail(session, recipient, subject, body, attach) |
|
246 |
finally: |
|
247 |
_logoff(session) |
|
248 |
finally: |
|
249 |
os.chdir(restore) |
|
250 |
||
251 |
||
252 |
if __name__ == '__main__': |
|
253 |
import sys |
|
254 |
recipient = "test@johnnypops.demon.co.uk" |
|
255 |
subject = "Test Message Subject" |
|
256 |
body = "Hi,\r\n\r\nthis is a quick test message,\r\n\r\ncheers,\r\nJohn." |
|
257 |
attachment = sys.argv[0] |
|
258 |
SendMail(recipient, subject, body, attachment) |
|
2681.3.6
by Lukáš Lalinsky
New version of simplemapi.py with MIT license. |
259 |