/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/util/simplemapi.py

  • Committer: Lukáš Lalinsky
  • Date: 2007-08-12 21:08:27 UTC
  • mto: (2681.1.26 send-bundle)
  • mto: This revision was merged to the branch mainline in revision 2736.
  • Revision ID: lalinsky@gmail.com-20070812210827-sfljgm5tvv6f0k9j
New version of simplemapi.py with MIT license.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
"""
2
 
Based on this original script: http://www.kirbyfooty.com/simplemapi.py
3
 
 
4
 
Now works (and tested) with:
5
 
    Outlook Express, Outlook 97 and 2000, 
6
 
    Eudora, Incredimail and Mozilla Thunderbird (1.5.0.2)
7
 
 
8
 
Date   : 30 May 2006
9
 
Version: 1.0.0
10
 
 
11
 
John Popplewell
12
 
john@johnnypops.demon.co.uk
13
 
http://www.johnnypops.demon.co.uk/python/
14
 
 
15
 
Thanks to Werner F. Bruhin and Michele Petrazzo on the ctypes list.
 
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:
 
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.
 
34
 
 
35
If you have any bug-fixes, enhancements or suggestions regarding this 
 
36
software, please contact me at the above email address.
16
37
"""
17
38
 
18
39
import os
23
44
LPLHANDLE = POINTER(LHANDLE)
24
45
 
25
46
# Return codes
26
 
SUCCESS_SUCCESS = 0
27
 
MAPI_USER_ABORT = 1
 
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
 
61
MAPI_E_ATTACHMENT_WRITE_FAILURE = 13
 
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
28
76
# Recipient class
29
77
MAPI_ORIG       = 0
30
78
MAPI_TO         = 1
94
142
MAPILogoff.argtypes     = (LHANDLE, c_ulong, FLAGS, c_ulong)
95
143
 
96
144
 
 
145
class MAPIError(WindowsError):
 
146
 
 
147
    def __init__(self, code):
 
148
        WindowsError.__init__(self)
 
149
        self.code = code
 
150
 
 
151
    def __str__(self):
 
152
        return 'MAPI error %d' % (self.code,)
 
153
 
 
154
 
97
155
def _logon(profileName=None, password=None):
98
156
    pSession = LHANDLE()
99
157
    rc = MAPILogon(0, profileName, password, MAPI_LOGON_UI, 0, byref(pSession))
100
158
    if rc != SUCCESS_SUCCESS:
101
 
        raise WindowsError, "MAPI error %i" % rc
 
159
        raise MAPIError, rc
102
160
    return pSession
103
161
 
104
162
 
105
163
def _logoff(session):
106
164
    rc = MAPILogoff(session, 0, 0, 0)
107
165
    if rc != SUCCESS_SUCCESS:
108
 
        raise WindowsError, "MAPI error %i" % rc
 
166
        raise MAPIError, rc
109
167
 
110
168
 
111
169
def _resolveName(session, name):
112
170
    pRecipDesc = lpMapiRecipDesc()
113
171
    rc = MAPIResolveName(session, 0, name, 0, 0, byref(pRecipDesc))
114
172
    if rc != SUCCESS_SUCCESS:
115
 
        raise WindowsError, "MAPI error %i" % rc
 
173
        raise MAPIError, rc
116
174
    rd = pRecipDesc.contents
117
175
    name, address = rd.lpszName, rd.lpszAddress
118
176
    rc = MAPIFreeBuffer(pRecipDesc)
119
177
    if rc != SUCCESS_SUCCESS:
120
 
        raise WindowsError, "MAPI error %i" % rc
 
178
        raise MAPIError, rc
121
179
    return name, address
122
180
 
123
181
 
158
216
                      nFileCount, lpFiles)
159
217
 
160
218
    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
 
219
    if rc != SUCCESS_SUCCESS:
 
220
        raise MAPIError, rc
163
221
 
164
222
 
165
223
def SendMail(recipient, subject="", body="", attachfiles=""):
196
254
    body = "Hi,\r\n\r\nthis is a quick test message,\r\n\r\ncheers,\r\nJohn."
197
255
    attachment = sys.argv[0]
198
256
    SendMail(recipient, subject, body, attachment)
 
257