/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: Marius Kruger
  • Date: 2010-07-10 21:28:56 UTC
  • mto: (5384.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5385.
  • Revision ID: marius.kruger@enerweb.co.za-20100710212856-uq4ji3go0u5se7hx
* Update documentation
* add NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
software, please contact me at the above email address.
37
37
"""
38
38
 
39
 
from __future__ import absolute_import
40
 
 
41
39
import os
42
40
from ctypes import *
43
41
 
46
44
LPLHANDLE = POINTER(LHANDLE)
47
45
 
48
46
# Return codes
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
 
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
63
61
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
 
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
78
76
# Recipient class
79
 
MAPI_ORIG = 0
80
 
MAPI_TO = 1
 
77
MAPI_ORIG       = 0
 
78
MAPI_TO         = 1
81
79
# Send flags
82
 
MAPI_LOGON_UI = 1
83
 
MAPI_DIALOG = 8
84
 
 
 
80
MAPI_LOGON_UI   = 1
 
81
MAPI_DIALOG     = 8
85
82
 
86
83
class MapiRecipDesc(Structure):
87
84
    _fields_ = [
88
 
        ('ulReserved', c_ulong),
89
 
        ('ulRecipClass', c_ulong),
90
 
        ('lpszName', c_char_p),
91
 
        ('lpszAddress', c_char_p),
92
 
        ('ulEIDSize', c_ulong),
93
 
        ('lpEntryID', c_void_p),
 
85
        ('ulReserved',      c_ulong),
 
86
        ('ulRecipClass',    c_ulong),
 
87
        ('lpszName',        c_char_p),
 
88
        ('lpszAddress',     c_char_p),
 
89
        ('ulEIDSize',       c_ulong),
 
90
        ('lpEntryID',       c_void_p),
94
91
    ]
95
 
 
96
 
 
97
 
lpMapiRecipDesc = POINTER(MapiRecipDesc)
 
92
lpMapiRecipDesc  = POINTER(MapiRecipDesc)
98
93
lppMapiRecipDesc = POINTER(lpMapiRecipDesc)
99
94
 
100
 
 
101
95
class MapiFileDesc(Structure):
102
96
    _fields_ = [
103
 
        ('ulReserved', c_ulong),
104
 
        ('flFlags', c_ulong),
105
 
        ('nPosition', c_ulong),
106
 
        ('lpszPathName', c_char_p),
107
 
        ('lpszFileName', c_char_p),
108
 
        ('lpFileType', c_void_p),
 
97
        ('ulReserved',      c_ulong),
 
98
        ('flFlags',         c_ulong),
 
99
        ('nPosition',       c_ulong),
 
100
        ('lpszPathName',    c_char_p),
 
101
        ('lpszFileName',    c_char_p),
 
102
        ('lpFileType',      c_void_p),
109
103
    ]
110
 
 
111
 
 
112
104
lpMapiFileDesc = POINTER(MapiFileDesc)
113
105
 
114
 
 
115
106
class MapiMessage(Structure):
116
107
    _fields_ = [
117
 
        ('ulReserved', c_ulong),
118
 
        ('lpszSubject', c_char_p),
119
 
        ('lpszNoteText', c_char_p),
120
 
        ('lpszMessageType', c_char_p),
121
 
        ('lpszDateReceived', c_char_p),
122
 
        ('lpszConversationID', c_char_p),
123
 
        ('flFlags', FLAGS),
124
 
        ('lpOriginator', lpMapiRecipDesc),
125
 
        ('nRecipCount', c_ulong),
126
 
        ('lpRecips', lpMapiRecipDesc),
127
 
        ('nFileCount', c_ulong),
128
 
        ('lpFiles', lpMapiFileDesc),
 
108
        ('ulReserved',          c_ulong),
 
109
        ('lpszSubject',         c_char_p),
 
110
        ('lpszNoteText',        c_char_p),
 
111
        ('lpszMessageType',     c_char_p),
 
112
        ('lpszDateReceived',    c_char_p),
 
113
        ('lpszConversationID',  c_char_p),
 
114
        ('flFlags',             FLAGS),
 
115
        ('lpOriginator',        lpMapiRecipDesc),
 
116
        ('nRecipCount',         c_ulong),
 
117
        ('lpRecips',            lpMapiRecipDesc),
 
118
        ('nFileCount',          c_ulong),
 
119
        ('lpFiles',             lpMapiFileDesc),
129
120
    ]
130
 
 
131
 
 
132
121
lpMapiMessage = POINTER(MapiMessage)
133
122
 
134
 
MAPI = windll.mapi32
135
 
MAPISendMail = MAPI.MAPISendMail
136
 
MAPISendMail.restype = c_ulong
137
 
MAPISendMail.argtypes = (LHANDLE, c_ulong, lpMapiMessage, FLAGS, c_ulong)
 
123
MAPI                    = windll.mapi32
 
124
MAPISendMail            = MAPI.MAPISendMail
 
125
MAPISendMail.restype    = c_ulong
 
126
MAPISendMail.argtypes   = (LHANDLE, c_ulong, lpMapiMessage, FLAGS, c_ulong)
138
127
 
139
 
MAPIResolveName = MAPI.MAPIResolveName
 
128
MAPIResolveName         = MAPI.MAPIResolveName
140
129
MAPIResolveName.restype = c_ulong
141
 
MAPIResolveName.argtypes = (
142
 
    LHANDLE, c_ulong, c_char_p, FLAGS, c_ulong, lppMapiRecipDesc)
 
130
MAPIResolveName.argtypes= (LHANDLE, c_ulong, c_char_p, FLAGS, c_ulong, lppMapiRecipDesc)
143
131
 
144
 
MAPIFreeBuffer = MAPI.MAPIFreeBuffer
145
 
MAPIFreeBuffer.restype = c_ulong
 
132
MAPIFreeBuffer          = MAPI.MAPIFreeBuffer
 
133
MAPIFreeBuffer.restype  = c_ulong
146
134
MAPIFreeBuffer.argtypes = (c_void_p, )
147
135
 
148
 
MAPILogon = MAPI.MAPILogon
149
 
MAPILogon.restype = c_ulong
150
 
MAPILogon.argtypes = (LHANDLE, c_char_p, c_char_p, FLAGS, c_ulong, LPLHANDLE)
 
136
MAPILogon               = MAPI.MAPILogon
 
137
MAPILogon.restype       = c_ulong
 
138
MAPILogon.argtypes      = (LHANDLE, c_char_p, c_char_p, FLAGS, c_ulong, LPLHANDLE)
151
139
 
152
 
MAPILogoff = MAPI.MAPILogoff
153
 
MAPILogoff.restype = c_ulong
154
 
MAPILogoff.argtypes = (LHANDLE, c_ulong, FLAGS, c_ulong)
 
140
MAPILogoff              = MAPI.MAPILogoff
 
141
MAPILogoff.restype      = c_ulong
 
142
MAPILogoff.argtypes     = (LHANDLE, c_ulong, FLAGS, c_ulong)
155
143
 
156
144
 
157
145
class MAPIError(WindowsError):
168
156
    pSession = LHANDLE()
169
157
    rc = MAPILogon(0, profileName, password, MAPI_LOGON_UI, 0, byref(pSession))
170
158
    if rc != SUCCESS_SUCCESS:
171
 
        raise MAPIError(rc)
 
159
        raise MAPIError, rc
172
160
    return pSession
173
161
 
174
162
 
175
163
def _logoff(session):
176
164
    rc = MAPILogoff(session, 0, 0, 0)
177
165
    if rc != SUCCESS_SUCCESS:
178
 
        raise MAPIError(rc)
 
166
        raise MAPIError, rc
179
167
 
180
168
 
181
169
def _resolveName(session, name):
182
170
    pRecipDesc = lpMapiRecipDesc()
183
171
    rc = MAPIResolveName(session, 0, name, 0, 0, byref(pRecipDesc))
184
172
    if rc != SUCCESS_SUCCESS:
185
 
        raise MAPIError(rc)
 
173
        raise MAPIError, rc
186
174
    rd = pRecipDesc.contents
187
175
    name, address = rd.lpszName, rd.lpszAddress
188
176
    rc = MAPIFreeBuffer(pRecipDesc)
189
177
    if rc != SUCCESS_SUCCESS:
190
 
        raise MAPIError(rc)
 
178
        raise MAPIError, rc
191
179
    return name, address
192
180
 
193
181
 
229
217
 
230
218
    rc = MAPISendMail(session, 0, byref(msg), MAPI_DIALOG, 0)
231
219
    if rc != SUCCESS_SUCCESS:
232
 
        raise MAPIError(rc)
 
220
        raise MAPIError, rc
233
221
 
234
222
 
235
223
def SendMail(recipient, subject="", body="", attachfiles=""):
243
231
 
244
232
    attach = []
245
233
    AttachWork = attachfiles.split(';')
246
 
    for f in AttachWork:
247
 
        if os.path.exists(f):
248
 
            attach.append(os.path.abspath(f))
 
234
    for file in AttachWork:
 
235
        if os.path.exists(file):
 
236
            attach.append(file)
 
237
    attach = map(os.path.abspath, attach)
249
238
 
250
239
    restore = os.getcwd()
251
240
    try:
265
254
    body = "Hi,\r\n\r\nthis is a quick test message,\r\n\r\ncheers,\r\nJohn."
266
255
    attachment = sys.argv[0]
267
256
    SendMail(recipient, subject, body, attachment)
 
257