Attachment 'simplemapi.py'
Download 1 # -*- coding: iso-8859-1 -*-#
2 #/usr/bin/env python
3 """simplemapi.py created by Ian Cook
4
5 *Syntax*
6 simplemapi.SendMail(recipient,subject,body,attachment)
7
8 where:
9 recipient - string: address to send to (multiple address sperated
10 with a semicolin)
11 subject - string: subject header
12 body - string: message text
13 attach - string: files to attach (multiple attachments sperated with
14 a semicolin)
15
16 *Example usage*
17 import simplemapi
18 simplemapi.SendMail("to1address at server.com;to2address at server.com","My
19 Subject","My message body","c:\attachment1.txt;c:\attachment2")
20
21
22 I hope it is of some help to others.
23
24 You can download the script from http://www.kirbyfooty.com/simplemapi.py
25 <http://www.kirbyfooty.com/simplemapi.py>
26
27 Kind regards
28 Ian Cook
29 www.kirbyfooty.com <http://www.kirbyfooty.com>
30 """
31
32 import os
33 from ctypes import *
34
35 FLAGS = c_ulong
36 LHANDLE = c_ulong
37 LPLHANDLE = POINTER(LHANDLE)
38
39 # Return codes
40 SUCCESS_SUCCESS = 0
41 # Recipient class
42 MAPI_ORIG = 0
43 MAPI_TO = 1
44 # Send flags
45 MAPI_LOGON_UI = 1
46 MAPI_DIALOG = 8
47
48 class MapiRecipDesc(Structure):
49 _fields_ = [
50 ('ulReserved', c_ulong),
51 ('ulRecipClass', c_ulong),
52 ('lpszName', c_char_p),
53 ('lpszAddress', c_char_p),
54 ('ulEIDSize', c_ulong),
55 ('lpEntryID', c_void_p),
56 ]
57 lpMapiRecipDesc = POINTER(MapiRecipDesc)
58 lppMapiRecipDesc = POINTER(lpMapiRecipDesc)
59
60 class MapiFileDesc(Structure):
61 _fields_ = [
62 ('ulReserved', c_ulong),
63 ('flFlags', c_ulong),
64 ('nPosition', c_ulong),
65 ('lpszPathName', c_char_p),
66 ('lpszFileName', c_char_p),
67 ('lpFileType', c_void_p),
68 ]
69 lpMapiFileDesc = POINTER(MapiFileDesc)
70
71 class MapiMessage(Structure):
72 _fields_ = [
73 ('ulReserved', c_ulong),
74 ('lpszSubject', c_char_p),
75 ('lpszNoteText', c_char_p),
76 ('lpszMessageType', c_char_p),
77 ('lpszDateReceived', c_char_p),
78 ('lpszConversationID', c_char_p),
79 ('flFlags', FLAGS),
80 ('lpOriginator', lpMapiRecipDesc),
81 ('nRecipCount', c_ulong),
82 ('lpRecips', lpMapiRecipDesc),
83 ('nFileCount', c_ulong),
84 ('lpFiles', lpMapiFileDesc),
85 ]
86 lpMapiMessage = POINTER(MapiMessage)
87
88 MAPI = windll.mapi32
89 MAPISendMail = MAPI.MAPISendMail
90 MAPISendMail.restype = c_ulong
91 MAPISendMail.argtypes = (LHANDLE, c_ulong, lpMapiMessage, FLAGS, c_ulong)
92
93 MAPIResolveName = MAPI.MAPIResolveName
94 MAPIResolveName.restype = c_ulong
95 MAPIResolveName.argtypes= (LHANDLE, c_ulong, c_char_p, FLAGS, c_ulong, lppMapiRecipDesc)
96
97 MAPIFreeBuffer = MAPI.MAPIFreeBuffer
98 MAPIFreeBuffer.restype = c_ulong
99 MAPIFreeBuffer.argtypes = (c_void_p, )
100
101 MAPILogon = MAPI.MAPILogon
102 MAPILogon.restype = c_ulong
103 MAPILogon.argtypes = (LHANDLE, c_char_p, c_char_p, FLAGS, c_ulong, LPLHANDLE)
104
105 MAPILogoff = MAPI.MAPILogoff
106 MAPILogoff.restype = c_ulong
107 MAPILogoff.argtypes = (LHANDLE, c_ulong, FLAGS, c_ulong)
108
109
110 def _logon(profileName=None, password=None):
111 pSession = LHANDLE()
112 rc = MAPILogon(0, profileName, password, MAPI_LOGON_UI, 0, byref(pSession))
113 if rc != SUCCESS_SUCCESS:
114 raise WindowsError, "MAPI error %i" % rc
115 return pSession
116
117
118 def _logoff(session):
119 rc = MAPILogoff(session, 0, 0, 0)
120 if rc != SUCCESS_SUCCESS:
121 raise WindowsError, "MAPI error %i" % rc
122
123
124 def _resolveName(session, name):
125 pRecipDesc = lpMapiRecipDesc()
126 rc = MAPIResolveName(session, 0, name, 0, 0, byref(pRecipDesc))
127 if rc != SUCCESS_SUCCESS:
128 raise WindowsError, "MAPI error %i" % rc
129 rd = pRecipDesc.contents
130 name, address = rd.lpszName, rd.lpszAddress
131 rc = MAPIFreeBuffer(pRecipDesc)
132 if rc != SUCCESS_SUCCESS:
133 raise WindowsError, "MAPI error %i" % rc
134 return name, address
135
136
137 def _sendMail(session, recipient, subject, body, attach):
138 nFileCount = len(attach)
139 if attach:
140 MapiFileDesc_A = MapiFileDesc * len(attach)
141 fda = MapiFileDesc_A()
142 for fd, fa in zip(fda, attach):
143 fd.ulReserved = 0
144 fd.flFlags = 0
145 fd.nPosition = -1
146 fd.lpszPathName = fa
147 fd.lpszFileName = None
148 fd.lpFileType = None
149 lpFiles = fda
150 else:
151 lpFiles = lpMapiFileDesc()
152
153 RecipWork = recipient
154 RecipCnt = len(RecipWork)
155 MapiRecipDesc_A = MapiRecipDesc * len(RecipWork)
156 rda = MapiRecipDesc_A()
157 for rd, ra in zip(rda, RecipWork):
158 rd.ulReserved = 0
159 rd.ulRecipClass = ra[0]
160 try:
161 rd.lpszName, rd.lpszAddress = _resolveName(session, ra[1])
162 except WindowsError:
163 rd.lpszName = None
164 rd.lpszAddress = ra[1]
165 rd.ulEIDSize = 0
166 rd.lpEntryID = None
167 recip = rda
168
169 msg = MapiMessage(0, subject, body, None, None, None, 0, lpMapiRecipDesc(),
170 RecipCnt, recip,
171 nFileCount, lpFiles)
172
173 rc = MAPISendMail(session, 0, byref(msg), MAPI_DIALOG, 0)
174 if rc != SUCCESS_SUCCESS:
175 raise WindowsError, "MAPI error %i" % rc
176
177
178 def SendMail(recipient=[], subject="", body="", attachfiles=[]):
179 """Post an e-mail message using Simple MAPI
180
181 recipient - list of addresses
182 subject - string: subject header
183 body - string: message text
184 attach - list of files to attach
185 """
186
187 attach = []
188 for file in attachfiles:
189 if os.path.exists(file):
190 attach.append(file)
191 attach = map(os.path.abspath, attach)
192
193 restore = os.getcwd()
194 try:
195 session = _logon()
196 try:
197 _sendMail(session, recipient, subject, body, attach)
198 finally:
199 _logoff(session)
200 finally:
201 os.chdir(restore)
202
203
204 if __name__ == '__main__':
205 recipient = [(1, 'someoneto@free.fr'), (2, 'someonecc@free.fr'), (3, 'someonebcc@free.fr')]
206 subject = "Test Message Subject"
207 body = "Hi,\r\n\r\nthis is a quick test message,\r\n\r\ncheers,\r\nJohn."
208 attachment = ['simplemapi.py']
209 SendMail(recipient, subject, body, attachment)
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.