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,
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
55
raise NotImplementedError
57
def compose_merge_request(self, to, subject, directive):
58
"""Compose (and possibly send) a merge request
60
:param to: The address to send the request to
61
:param subject: The subject line to use for the request
62
:param directive: A merge directive representing the merge request, as
65
prompt = self._get_merge_prompt("Please describe these changes:", to,
67
self.compose(prompt, to, subject, directive,
70
def _get_merge_prompt(self, prompt, to, subject, attachment):
71
"""Generate a prompt string. Overridden by Editor.
73
:param prompt: A string suggesting what user should do
74
:param to: The address the mail will be sent to
75
:param subject: The subject line of the mail
76
:param attachment: The attachment that will be used
81
class Editor(MailClient):
82
"""DIY mail client that uses commit message editor"""
84
def _get_merge_prompt(self, prompt, to, subject, attachment):
85
"""See MailClient._get_merge_prompt"""
89
u"%s" % (prompt, to, subject,
90
attachment.decode('utf-8', 'replace')))
92
def compose(self, prompt, to, subject, attachment, mime_subtype,
94
"""See MailClient.compose"""
96
raise errors.NoMailAddressSpecified()
97
body = msgeditor.edit_commit_message(prompt)
99
raise errors.NoMessageSupplied()
100
email_message.EmailMessage.send(self.config,
101
self.config.username(),
106
attachment_mime_subtype=mime_subtype)
109
class ExternalMailClient(MailClient):
110
"""An external mail client."""
112
def _get_client_commands(self):
113
"""Provide a list of commands that may invoke the mail client"""
114
if sys.platform == 'win32':
116
return [win32utils.get_app_path(i) for i in self._client_commands]
118
return self._client_commands
120
def compose(self, prompt, to, subject, attachment, mime_subtype,
122
"""See MailClient.compose.
124
Writes the attachment to a temporary file, invokes _compose.
126
fd, pathname = tempfile.mkstemp(extension, 'bzr-mail-')
128
os.write(fd, attachment)
131
self._compose(prompt, to, subject, pathname, mime_subtype, extension)
133
def _compose(self, prompt, to, subject, attach_path, mime_subtype,
135
"""Invoke a mail client as a commandline process.
137
Overridden by MAPIClient.
138
:param to: The address to send the mail to
139
:param subject: The subject line for the mail
140
:param pathname: The path to the attachment
141
:param mime_subtype: The attachment is assumed to have a major type of
142
"text", but the precise subtype can be specified here
143
:param extension: A file extension (including period) associated with
146
for name in self._get_client_commands():
147
cmdline = _get_compose_8bit_commandline(name, to, subject,
150
subprocess.call(cmdline)
152
if e.errno != errno.ENOENT:
157
raise errors.MailClientNotFound(self._client_commands)
159
def _get_compose_commandline(self, to, subject, attach_path):
160
"""Determine the commandline to use for composing a message
162
Implemented by various subclasses
163
:param to: The address to send the mail to
164
:param subject: The subject line for the mail
165
:param attach_path: The path to the attachment
167
raise NotImplementedError
169
def _get_compose_8bit_commandline(self, name, to, subject, attach_path):
170
"""Wrapper around _get_compose_commandline()
171
to ensure that resulting command line is plain string.
173
:param name: name of external mail client (first argument).
176
user_encoding = osutils.get_user_encoding()
178
to = to.encode(user_encoding, 'replace')
180
subject = subject.encode(user_encoding, 'replace')
181
cmdline.extend(self._get_compose_commandline(to, subject,
186
class Evolution(ExternalMailClient):
187
"""Evolution mail client."""
189
_client_commands = ['evolution']
191
def _get_compose_commandline(self, to, subject, attach_path):
192
"""See ExternalMailClient._get_compose_commandline"""
194
if subject is not None:
195
message_options['subject'] = subject
196
if attach_path is not None:
197
message_options['attach'] = attach_path
198
options_list = ['%s=%s' % (k, urlutils.escape(v)) for (k, v) in
199
message_options.iteritems()]
200
return ['mailto:%s?%s' % (to or '', '&'.join(options_list))]
203
class Mutt(ExternalMailClient):
204
"""Mutt mail client."""
206
_client_commands = ['mutt']
208
def _get_compose_commandline(self, to, subject, attach_path):
209
"""See ExternalMailClient._get_compose_commandline"""
211
if subject is not None:
212
message_options.extend(['-s', subject ])
213
if attach_path is not None:
214
message_options.extend(['-a', attach_path])
216
message_options.append(to)
217
return message_options
220
class Thunderbird(ExternalMailClient):
221
"""Mozilla Thunderbird (or Icedove)
223
Note that Thunderbird 1.5 is buggy and does not support setting
224
"to" simultaneously with including a attachment.
226
There is a workaround if no attachment is present, but we always need to
230
_client_commands = ['thunderbird', 'mozilla-thunderbird', 'icedove',
231
'/Applications/Mozilla/Thunderbird.app/Contents/MacOS/thunderbird-bin']
233
def _get_compose_commandline(self, to, subject, attach_path):
234
"""See ExternalMailClient._get_compose_commandline"""
237
message_options['to'] = to
238
if subject is not None:
239
message_options['subject'] = subject
240
if attach_path is not None:
241
message_options['attachment'] = urlutils.local_path_to_url(
243
options_list = ["%s='%s'" % (k, v) for k, v in
244
sorted(message_options.iteritems())]
245
return ['-compose', ','.join(options_list)]
248
class KMail(ExternalMailClient):
249
"""KDE mail client."""
251
_client_commands = ['kmail']
253
def _get_compose_commandline(self, to, subject, attach_path):
254
"""See ExternalMailClient._get_compose_commandline"""
256
if subject is not None:
257
message_options.extend( ['-s', subject ] )
258
if attach_path is not None:
259
message_options.extend( ['--attach', attach_path] )
261
message_options.extend( [ to ] )
263
return message_options
266
class XDGEmail(ExternalMailClient):
267
"""xdg-email attempts to invoke the user's preferred mail client"""
269
_client_commands = ['xdg-email']
271
def _get_compose_commandline(self, to, subject, attach_path):
272
"""See ExternalMailClient._get_compose_commandline"""
274
raise errors.NoMailAddressSpecified()
276
if subject is not None:
277
commandline.extend(['--subject', subject])
278
if attach_path is not None:
279
commandline.extend(['--attach', attach_path])
283
class MAPIClient(ExternalMailClient):
284
"""Default Windows mail client launched using MAPI."""
286
def _compose(self, prompt, to, subject, attach_path, mime_subtype,
288
"""See ExternalMailClient._compose.
290
This implementation uses MAPI via the simplemapi ctypes wrapper
292
from bzrlib.util import simplemapi
294
simplemapi.SendMail(to or '', subject or '', '', attach_path)
295
except simplemapi.MAPIError, e:
296
if e.code != simplemapi.MAPI_USER_ABORT:
297
raise errors.MailClientNotFound(['MAPI supported mail client'
298
' (error %d)' % (e.code,)])
301
class DefaultMail(MailClient):
302
"""Default mail handling. Tries XDGEmail (or MAPIClient on Windows),
303
falls back to Editor"""
305
def _mail_client(self):
306
"""Determine the preferred mail client for this platform"""
307
if osutils.supports_mapi():
308
return MAPIClient(self.config)
310
return XDGEmail(self.config)
312
def compose(self, prompt, to, subject, attachment, mime_subtype,
314
"""See MailClient.compose"""
316
return self._mail_client().compose(prompt, to, subject,
317
attachment, mimie_subtype,
319
except errors.MailClientNotFound:
320
return Editor(self.config).compose(prompt, to, subject,
321
attachment, mimie_subtype, extension)
323
def compose_merge_request(self, to, subject, directive):
324
"""See MailClient.compose_merge_request"""
326
return self._mail_client().compose_merge_request(to, subject,
328
except errors.MailClientNotFound:
329
return Editor(self.config).compose_merge_request(to, subject,