/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
39
import os
40
from ctypes import *
41
42
FLAGS = c_ulong
43
LHANDLE = c_ulong
44
LPLHANDLE = POINTER(LHANDLE)
45
46
# Return codes
7143.15.2 by Jelmer Vernooij
Run autopep8.
47
SUCCESS_SUCCESS = 0
48
MAPI_USER_ABORT = 1
49
MAPI_E_USER_ABORT = MAPI_USER_ABORT
50
MAPI_E_FAILURE = 2
51
MAPI_E_LOGON_FAILURE = 3
52
MAPI_E_LOGIN_FAILURE = MAPI_E_LOGON_FAILURE
53
MAPI_E_DISK_FULL = 4
54
MAPI_E_INSUFFICIENT_MEMORY = 5
55
MAPI_E_ACCESS_DENIED = 6
56
MAPI_E_TOO_MANY_SESSIONS = 8
57
MAPI_E_TOO_MANY_FILES = 9
58
MAPI_E_TOO_MANY_RECIPIENTS = 10
59
MAPI_E_ATTACHMENT_NOT_FOUND = 11
60
MAPI_E_ATTACHMENT_OPEN_FAILURE = 12
2681.3.6 by Lukáš Lalinsky
New version of simplemapi.py with MIT license.
61
MAPI_E_ATTACHMENT_WRITE_FAILURE = 13
7143.15.2 by Jelmer Vernooij
Run autopep8.
62
MAPI_E_UNKNOWN_RECIPIENT = 14
63
MAPI_E_BAD_RECIPTYPE = 15
64
MAPI_E_NO_MESSAGES = 16
65
MAPI_E_INVALID_MESSAGE = 17
66
MAPI_E_TEXT_TOO_LARGE = 18
67
MAPI_E_INVALID_SESSION = 19
68
MAPI_E_TYPE_NOT_SUPPORTED = 20
69
MAPI_E_AMBIGUOUS_RECIPIENT = 21
70
MAPI_E_AMBIG_RECIP = MAPI_E_AMBIGUOUS_RECIPIENT
71
MAPI_E_MESSAGE_IN_USE = 22
72
MAPI_E_NETWORK_FAILURE = 23
73
MAPI_E_INVALID_EDITFIELDS = 24
74
MAPI_E_INVALID_RECIPS = 25
75
MAPI_E_NOT_SUPPORTED = 26
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
76
# Recipient class
7143.15.2 by Jelmer Vernooij
Run autopep8.
77
MAPI_ORIG = 0
78
MAPI_TO = 1
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
79
# Send flags
7143.15.2 by Jelmer Vernooij
Run autopep8.
80
MAPI_LOGON_UI = 1
81
MAPI_DIALOG = 8
82
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
83
84
class MapiRecipDesc(Structure):
85
    _fields_ = [
7143.15.2 by Jelmer Vernooij
Run autopep8.
86
        ('ulReserved', c_ulong),
87
        ('ulRecipClass', c_ulong),
88
        ('lpszName', c_char_p),
89
        ('lpszAddress', c_char_p),
90
        ('ulEIDSize', c_ulong),
91
        ('lpEntryID', c_void_p),
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
92
    ]
7143.15.2 by Jelmer Vernooij
Run autopep8.
93
94
95
lpMapiRecipDesc = POINTER(MapiRecipDesc)
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
96
lppMapiRecipDesc = POINTER(lpMapiRecipDesc)
97
7143.15.2 by Jelmer Vernooij
Run autopep8.
98
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
99
class MapiFileDesc(Structure):
100
    _fields_ = [
7143.15.2 by Jelmer Vernooij
Run autopep8.
101
        ('ulReserved', c_ulong),
102
        ('flFlags', c_ulong),
103
        ('nPosition', c_ulong),
104
        ('lpszPathName', c_char_p),
105
        ('lpszFileName', c_char_p),
106
        ('lpFileType', c_void_p),
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
107
    ]
7143.15.2 by Jelmer Vernooij
Run autopep8.
108
109
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
110
lpMapiFileDesc = POINTER(MapiFileDesc)
111
7143.15.2 by Jelmer Vernooij
Run autopep8.
112
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
113
class MapiMessage(Structure):
114
    _fields_ = [
7143.15.2 by Jelmer Vernooij
Run autopep8.
115
        ('ulReserved', c_ulong),
116
        ('lpszSubject', c_char_p),
117
        ('lpszNoteText', c_char_p),
118
        ('lpszMessageType', c_char_p),
119
        ('lpszDateReceived', c_char_p),
120
        ('lpszConversationID', c_char_p),
121
        ('flFlags', FLAGS),
122
        ('lpOriginator', lpMapiRecipDesc),
123
        ('nRecipCount', c_ulong),
124
        ('lpRecips', lpMapiRecipDesc),
125
        ('nFileCount', c_ulong),
126
        ('lpFiles', lpMapiFileDesc),
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
127
    ]
7143.15.2 by Jelmer Vernooij
Run autopep8.
128
129
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
130
lpMapiMessage = POINTER(MapiMessage)
131
7143.15.2 by Jelmer Vernooij
Run autopep8.
132
MAPI = windll.mapi32
133
MAPISendMail = MAPI.MAPISendMail
134
MAPISendMail.restype = c_ulong
135
MAPISendMail.argtypes = (LHANDLE, c_ulong, lpMapiMessage, FLAGS, c_ulong)
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
136
7143.15.2 by Jelmer Vernooij
Run autopep8.
137
MAPIResolveName = MAPI.MAPIResolveName
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
138
MAPIResolveName.restype = c_ulong
7143.15.2 by Jelmer Vernooij
Run autopep8.
139
MAPIResolveName.argtypes = (
140
    LHANDLE, c_ulong, c_char_p, FLAGS, c_ulong, lppMapiRecipDesc)
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
141
7143.15.2 by Jelmer Vernooij
Run autopep8.
142
MAPIFreeBuffer = MAPI.MAPIFreeBuffer
143
MAPIFreeBuffer.restype = c_ulong
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
144
MAPIFreeBuffer.argtypes = (c_void_p, )
145
7143.15.2 by Jelmer Vernooij
Run autopep8.
146
MAPILogon = MAPI.MAPILogon
147
MAPILogon.restype = c_ulong
148
MAPILogon.argtypes = (LHANDLE, c_char_p, c_char_p, FLAGS, c_ulong, LPLHANDLE)
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
149
7143.15.2 by Jelmer Vernooij
Run autopep8.
150
MAPILogoff = MAPI.MAPILogoff
151
MAPILogoff.restype = c_ulong
152
MAPILogoff.argtypes = (LHANDLE, c_ulong, FLAGS, c_ulong)
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
153
154
2681.3.6 by Lukáš Lalinsky
New version of simplemapi.py with MIT license.
155
class MAPIError(WindowsError):
156
157
    def __init__(self, code):
158
        WindowsError.__init__(self)
159
        self.code = code
160
161
    def __str__(self):
162
        return 'MAPI error %d' % (self.code,)
163
164
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
165
def _logon(profileName=None, password=None):
166
    pSession = LHANDLE()
167
    rc = MAPILogon(0, profileName, password, MAPI_LOGON_UI, 0, byref(pSession))
168
    if rc != SUCCESS_SUCCESS:
6619.3.11 by Jelmer Vernooij
Use modern exceptions.
169
        raise MAPIError(rc)
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
170
    return pSession
171
172
173
def _logoff(session):
174
    rc = MAPILogoff(session, 0, 0, 0)
175
    if rc != SUCCESS_SUCCESS:
6619.3.11 by Jelmer Vernooij
Use modern exceptions.
176
        raise MAPIError(rc)
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
177
178
179
def _resolveName(session, name):
180
    pRecipDesc = lpMapiRecipDesc()
181
    rc = MAPIResolveName(session, 0, name, 0, 0, byref(pRecipDesc))
182
    if rc != SUCCESS_SUCCESS:
6619.3.11 by Jelmer Vernooij
Use modern exceptions.
183
        raise MAPIError(rc)
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
184
    rd = pRecipDesc.contents
185
    name, address = rd.lpszName, rd.lpszAddress
186
    rc = MAPIFreeBuffer(pRecipDesc)
187
    if rc != SUCCESS_SUCCESS:
6619.3.11 by Jelmer Vernooij
Use modern exceptions.
188
        raise MAPIError(rc)
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
189
    return name, address
190
191
192
def _sendMail(session, recipient, subject, body, attach):
193
    nFileCount = len(attach)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
194
    if attach:
195
        MapiFileDesc_A = MapiFileDesc * len(attach)
196
        fda = MapiFileDesc_A()
197
        for fd, fa in zip(fda, attach):
198
            fd.ulReserved = 0
199
            fd.flFlags = 0
200
            fd.nPosition = -1
201
            fd.lpszPathName = fa
202
            fd.lpszFileName = None
203
            fd.lpFileType = None
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
204
        lpFiles = fda
205
    else:
206
        lpFiles = lpMapiFileDesc()
207
208
    RecipWork = recipient.split(';')
209
    RecipCnt = len(RecipWork)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
210
    MapiRecipDesc_A = MapiRecipDesc * len(RecipWork)
211
    rda = MapiRecipDesc_A()
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
212
    for rd, ra in zip(rda, RecipWork):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
213
        rd.ulReserved = 0
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
214
        rd.ulRecipClass = MAPI_TO
215
        try:
216
            rd.lpszName, rd.lpszAddress = _resolveName(session, ra)
217
        except WindowsError:
218
            # work-round for Mozilla Thunderbird
219
            rd.lpszName, rd.lpszAddress = None, ra
220
        rd.ulEIDSize = 0
221
        rd.lpEntryID = None
222
    recip = rda
223
224
    msg = MapiMessage(0, subject, body, None, None, None, 0, lpMapiRecipDesc(),
225
                      RecipCnt, recip,
226
                      nFileCount, lpFiles)
227
228
    rc = MAPISendMail(session, 0, byref(msg), MAPI_DIALOG, 0)
2681.3.6 by Lukáš Lalinsky
New version of simplemapi.py with MIT license.
229
    if rc != SUCCESS_SUCCESS:
6619.3.11 by Jelmer Vernooij
Use modern exceptions.
230
        raise MAPIError(rc)
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
231
232
233
def SendMail(recipient, subject="", body="", attachfiles=""):
234
    """Post an e-mail message using Simple MAPI
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
235
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
236
    recipient - string: address to send to (multiple addresses separated with a semicolon)
237
    subject   - string: subject header
238
    body      - string: message text
239
    attach    - string: files to attach (multiple attachments separated with a semicolon)
240
    """
241
242
    attach = []
243
    AttachWork = attachfiles.split(';')
6631.3.1 by Martin
Run 2to3 map fixer and refactor after
244
    for f in AttachWork:
245
        if os.path.exists(f):
246
            attach.append(os.path.abspath(f))
2681.3.3 by Lukáš Lalinsky
Convert to UNIX line endings.
247
248
    restore = os.getcwd()
249
    try:
250
        session = _logon()
251
        try:
252
            _sendMail(session, recipient, subject, body, attach)
253
        finally:
254
            _logoff(session)
255
    finally:
256
        os.chdir(restore)
257
258
259
if __name__ == '__main__':
260
    import sys
261
    recipient = "test@johnnypops.demon.co.uk"
262
    subject = "Test Message Subject"
263
    body = "Hi,\r\n\r\nthis is a quick test message,\r\n\r\ncheers,\r\nJohn."
264
    attachment = sys.argv[0]
265
    SendMail(recipient, subject, body, attachment)