1
# Copyright (C) 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32
class MailClient(object):
33
"""A mail client that can send messages with attachements."""
35
def __init__(self, config):
38
def compose(self, prompt, to, subject, attachment, mime_subtype,
39
extension, basename=None):
40
"""Compose (and possibly send) an email message
42
Must be implemented by subclasses.
44
:param prompt: A message to tell the user what to do. Supported by
45
the Editor client, but ignored by others
46
:param to: The address to send the message to
47
:param subject: The contents of the subject line
48
:param attachment: An email attachment, as a bytestring
49
:param mime_subtype: The attachment is assumed to be a subtype of
50
Text. This allows the precise subtype to be specified, e.g.
51
"plain", "x-patch", etc.
52
:param extension: The file extension associated with the attachment
54
:param basename: The name to use for the attachment, e.g.
57
raise NotImplementedError
59
def compose_merge_request(self, to, subject, directive, basename=None):
60
"""Compose (and possibly send) a merge request
62
:param to: The address to send the request to
63
:param subject: The subject line to use for the request
64
:param directive: A merge directive representing the merge request, as
66
:param basename: The name to use for the attachment, e.g.
69
prompt = self._get_merge_prompt("Please describe these changes:", to,
71
self.compose(prompt, to, subject, directive,
72
'x-patch', '.patch', basename)
74
def _get_merge_prompt(self, prompt, to, subject, attachment):
75
"""Generate a prompt string. Overridden by Editor.
77
:param prompt: A string suggesting what user should do
78
:param to: The address the mail will be sent to
79
:param subject: The subject line of the mail
80
:param attachment: The attachment that will be used
85
class Editor(MailClient):
86
"""DIY mail client that uses commit message editor"""
88
def _get_merge_prompt(self, prompt, to, subject, attachment):
89
"""See MailClient._get_merge_prompt"""
93
u"%s" % (prompt, to, subject,
94
attachment.decode('utf-8', 'replace')))
96
def compose(self, prompt, to, subject, attachment, mime_subtype,
97
extension, basename=None):
98
"""See MailClient.compose"""
100
raise errors.NoMailAddressSpecified()
101
body = msgeditor.edit_commit_message(prompt)
103
raise errors.NoMessageSupplied()
104
email_message.EmailMessage.send(self.config,
105
self.config.username(),
110
attachment_mime_subtype=mime_subtype)
113
class ExternalMailClient(MailClient):
114
"""An external mail client."""
116
def _get_client_commands(self):
117
"""Provide a list of commands that may invoke the mail client"""
118
if sys.platform == 'win32':
120
return [win32utils.get_app_path(i) for i in self._client_commands]
122
return self._client_commands
124
def compose(self, prompt, to, subject, attachment, mime_subtype,
125
extension, basename=None):
126
"""See MailClient.compose.
128
Writes the attachment to a temporary file, invokes _compose.
131
basename = 'attachment'
132
pathname = tempfile.mkdtemp(prefix='bzr-mail-')
133
attach_path = osutils.pathjoin(pathname, basename + extension)
134
outfile = open(attach_path, 'wb')
136
outfile.write(attachment)
139
self._compose(prompt, to, subject, attach_path, mime_subtype,
142
def _compose(self, prompt, to, subject, attach_path, mime_subtype,
144
"""Invoke a mail client as a commandline process.
146
Overridden by MAPIClient.
147
:param to: The address to send the mail to
148
:param subject: The subject line for the mail
149
:param pathname: The path to the attachment
150
:param mime_subtype: The attachment is assumed to have a major type of
151
"text", but the precise subtype can be specified here
152
:param extension: A file extension (including period) associated with
155
for name in self._get_client_commands():
157
cmdline.extend(self._get_compose_commandline(to, subject,
160
subprocess.call(cmdline)
162
if e.errno != errno.ENOENT:
167
raise errors.MailClientNotFound(self._client_commands)
169
def _get_compose_commandline(self, to, subject, attach_path):
170
"""Determine the commandline to use for composing a message
172
Implemented by various subclasses
173
:param to: The address to send the mail to
174
:param subject: The subject line for the mail
175
:param attach_path: The path to the attachment
177
raise NotImplementedError
180
class Evolution(ExternalMailClient):
181
"""Evolution mail client."""
183
_client_commands = ['evolution']
185
def _get_compose_commandline(self, to, subject, attach_path):
186
"""See ExternalMailClient._get_compose_commandline"""
188
if subject is not None:
189
message_options['subject'] = subject
190
if attach_path is not None:
191
message_options['attach'] = attach_path
192
options_list = ['%s=%s' % (k, urlutils.escape(v)) for (k, v) in
193
message_options.iteritems()]
194
return ['mailto:%s?%s' % (to or '', '&'.join(options_list))]
197
class Mutt(ExternalMailClient):
198
"""Mutt mail client."""
200
_client_commands = ['mutt']
202
def _get_compose_commandline(self, to, subject, attach_path):
203
"""See ExternalMailClient._get_compose_commandline"""
205
if subject is not None:
206
message_options.extend(['-s', subject ])
207
if attach_path is not None:
208
message_options.extend(['-a', attach_path])
210
message_options.append(to)
211
return message_options
214
class Thunderbird(ExternalMailClient):
215
"""Mozilla Thunderbird (or Icedove)
217
Note that Thunderbird 1.5 is buggy and does not support setting
218
"to" simultaneously with including a attachment.
220
There is a workaround if no attachment is present, but we always need to
224
_client_commands = ['thunderbird', 'mozilla-thunderbird', 'icedove',
225
'/Applications/Mozilla/Thunderbird.app/Contents/MacOS/thunderbird-bin']
227
def _get_compose_commandline(self, to, subject, attach_path):
228
"""See ExternalMailClient._get_compose_commandline"""
231
message_options['to'] = to
232
if subject is not None:
233
message_options['subject'] = subject
234
if attach_path is not None:
235
message_options['attachment'] = urlutils.local_path_to_url(
237
options_list = ["%s='%s'" % (k, v) for k, v in
238
sorted(message_options.iteritems())]
239
return ['-compose', ','.join(options_list)]
242
class KMail(ExternalMailClient):
243
"""KDE mail client."""
245
_client_commands = ['kmail']
247
def _get_compose_commandline(self, to, subject, attach_path):
248
"""See ExternalMailClient._get_compose_commandline"""
250
if subject is not None:
251
message_options.extend( ['-s', subject ] )
252
if attach_path is not None:
253
message_options.extend( ['--attach', attach_path] )
255
message_options.extend( [ to ] )
257
return message_options
260
class XDGEmail(ExternalMailClient):
261
"""xdg-email attempts to invoke the user's preferred mail client"""
263
_client_commands = ['xdg-email']
265
def _get_compose_commandline(self, to, subject, attach_path):
266
"""See ExternalMailClient._get_compose_commandline"""
268
raise errors.NoMailAddressSpecified()
270
if subject is not None:
271
commandline.extend(['--subject', subject])
272
if attach_path is not None:
273
commandline.extend(['--attach', attach_path])
277
class MAPIClient(ExternalMailClient):
278
"""Default Windows mail client launched using MAPI."""
280
def _compose(self, prompt, to, subject, attach_path, mime_subtype,
282
"""See ExternalMailClient._compose.
284
This implementation uses MAPI via the simplemapi ctypes wrapper
286
from bzrlib.util import simplemapi
288
simplemapi.SendMail(to or '', subject or '', '', attach_path)
289
except simplemapi.MAPIError, e:
290
if e.code != simplemapi.MAPI_USER_ABORT:
291
raise errors.MailClientNotFound(['MAPI supported mail client'
292
' (error %d)' % (e.code,)])
295
class DefaultMail(MailClient):
296
"""Default mail handling. Tries XDGEmail (or MAPIClient on Windows),
297
falls back to Editor"""
299
def _mail_client(self):
300
"""Determine the preferred mail client for this platform"""
301
if osutils.supports_mapi():
302
return MAPIClient(self.config)
304
return XDGEmail(self.config)
306
def compose(self, prompt, to, subject, attachment, mime_subtype,
307
extension, basename=None):
308
"""See MailClient.compose"""
310
return self._mail_client().compose(prompt, to, subject,
311
attachment, mimie_subtype,
313
except errors.MailClientNotFound:
314
return Editor(self.config).compose(prompt, to, subject,
315
attachment, mimie_subtype, extension)
317
def compose_merge_request(self, to, subject, directive):
318
"""See MailClient.compose_merge_request"""
320
return self._mail_client().compose_merge_request(to, subject,
322
except errors.MailClientNotFound:
323
return Editor(self.config).compose_merge_request(to, subject,