14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
from __future__ import absolute_import
27
config as _mod_config,
40
34
mail_client_registry = registry.Registry()
43
class MailClientNotFound(errors.BzrError):
45
_fmt = "Unable to find mail client with the following names:"\
46
" %(mail_command_list_string)s"
48
def __init__(self, mail_command_list):
49
mail_command_list_string = ', '.join(mail_command_list)
50
errors.BzrError.__init__(
51
self, mail_command_list=mail_command_list,
52
mail_command_list_string=mail_command_list_string)
55
class NoMessageSupplied(errors.BzrError):
57
_fmt = "No message supplied."
60
class NoMailAddressSpecified(errors.BzrError):
62
_fmt = "No mail-to address (--mail-to) or output (-o) specified."
65
37
class MailClient(object):
66
38
"""A mail client that can send messages with attachements."""
133
105
extension, basename=None, body=None):
134
106
"""See MailClient.compose"""
136
raise NoMailAddressSpecified()
108
raise errors.NoMailAddressSpecified()
137
109
body = msgeditor.edit_commit_message(prompt, start_message=body)
139
raise NoMessageSupplied()
111
raise errors.NoMessageSupplied()
140
112
email_message.EmailMessage.send(self.config,
141
self.config.get('email'),
113
self.config.username(),
146
118
attachment_mime_subtype=mime_subtype)
149
119
mail_client_registry.register('editor', Editor,
150
120
help=Editor.__doc__)
298
266
"""See ExternalMailClient._get_compose_commandline"""
299
267
message_options = []
300
268
if subject is not None:
301
message_options.extend(
302
['-s', self._encode_safe(subject)])
269
message_options.extend(['-s', self._encode_safe(subject)])
303
270
if attach_path is not None:
304
message_options.extend(
305
['-a', self._encode_path(attach_path, 'attachment')])
271
message_options.extend(['-a',
272
self._encode_path(attach_path, 'attachment')])
306
273
if body is not None:
307
274
# Store the temp file object in self, so that it does not get
308
275
# garbage collected and delete the file before mutt can read it.
309
276
self._temp_file = tempfile.NamedTemporaryFile(
310
prefix="mutt-body-", suffix=".txt", mode="w+")
277
prefix="mutt-body-", suffix=".txt")
311
278
self._temp_file.write(body)
312
279
self._temp_file.flush()
313
280
message_options.extend(['-i', self._temp_file.name])
314
281
if to is not None:
315
282
message_options.extend(['--', self._encode_safe(to)])
316
283
return message_options
319
284
mail_client_registry.register('mutt', Mutt,
320
285
help=Mutt.__doc__)
393
352
"""See ExternalMailClient._get_compose_commandline"""
395
354
if from_ is not None:
396
compose_url.append('from=' + urlutils.quote(from_))
355
compose_url.append('from=' + urllib.quote(from_))
397
356
if subject is not None:
398
# Don't use urlutils.quote_plus because Claws doesn't seem
357
# Don't use urllib.quote_plus because Claws doesn't seem
399
358
# to recognise spaces encoded as "+".
400
359
compose_url.append(
401
'subject=' + urlutils.quote(self._encode_safe(subject)))
360
'subject=' + urllib.quote(self._encode_safe(subject)))
402
361
if body is not None:
403
362
compose_url.append(
404
'body=' + urlutils.quote(self._encode_safe(body)))
363
'body=' + urllib.quote(self._encode_safe(body)))
405
364
# to must be supplied for the claws-mail --compose syntax to work.
407
raise NoMailAddressSpecified()
366
raise errors.NoMailAddressSpecified()
408
367
compose_url = 'mailto:%s?%s' % (
409
368
self._encode_safe(to), '&'.join(compose_url))
410
369
# Collect command-line options.
430
389
class XDGEmail(BodyExternalMailClient):
431
__doc__ = """xdg-email attempts to invoke the preferred mail client"""
390
__doc__ = """xdg-email attempts to invoke the user's preferred mail client"""
433
392
_client_commands = ['xdg-email']
435
394
def _get_compose_commandline(self, to, subject, attach_path, body=None):
436
395
"""See ExternalMailClient._get_compose_commandline"""
438
raise NoMailAddressSpecified()
397
raise errors.NoMailAddressSpecified()
439
398
commandline = [self._encode_safe(to)]
440
399
if subject is not None:
441
400
commandline.extend(['--subject', self._encode_safe(subject)])
442
401
if attach_path is not None:
443
402
commandline.extend(['--attach',
444
self._encode_path(attach_path, 'attachment')])
403
self._encode_path(attach_path, 'attachment')])
445
404
if body is not None:
446
405
commandline.extend(['--body', self._encode_safe(body)])
447
406
return commandline
450
407
mail_client_registry.register('xdg-email', XDGEmail,
451
408
help=XDGEmail.__doc__)
573
528
This implementation uses MAPI via the simplemapi ctypes wrapper
575
from .util import simplemapi
530
from bzrlib.util import simplemapi
577
532
simplemapi.SendMail(to or '', subject or '', body or '',
579
except simplemapi.MAPIError as e:
534
except simplemapi.MAPIError, e:
580
535
if e.code != simplemapi.MAPI_USER_ABORT:
581
raise MailClientNotFound(['MAPI supported mail client'
582
' (error %d)' % (e.code,)])
536
raise errors.MailClientNotFound(['MAPI supported mail client'
537
' (error %d)' % (e.code,)])
585
538
mail_client_registry.register('mapi', MAPIClient,
586
539
help=MAPIClient.__doc__)
599
552
_client_commands = ['osascript']
601
554
def _get_compose_commandline(self, to, subject, attach_path, body=None,
603
"""See ExternalMailClient._get_compose_commandline"""
605
fd, self.temp_file = tempfile.mkstemp(prefix="bzr-send-",
608
os.write(fd, 'tell application "Mail"\n')
609
os.write(fd, 'set newMessage to make new outgoing message\n')
610
os.write(fd, 'tell newMessage\n')
612
os.write(fd, 'make new to recipient with properties'
613
' {address:"%s"}\n' % to)
614
if from_ is not None:
615
# though from_ doesn't actually seem to be used
616
os.write(fd, 'set sender to "%s"\n'
617
% from_.replace('"', '\\"'))
618
if subject is not None:
619
os.write(fd, 'set subject to "%s"\n'
620
% subject.replace('"', '\\"'))
622
# FIXME: would be nice to prepend the body to the
623
# existing content (e.g., preserve signature), but
624
# can't seem to figure out the right applescript
626
os.write(fd, 'set content to "%s\\n\n"\n' %
627
body.replace('"', '\\"').replace('\n', '\\n'))
629
if attach_path is not None:
630
# FIXME: would be nice to first append a newline to
631
# ensure the attachment is on a new paragraph, but
632
# can't seem to figure out the right applescript
634
os.write(fd, 'tell content to make new attachment'
635
' with properties {file name:"%s"}'
636
' at after the last paragraph\n'
637
% self._encode_path(attach_path, 'attachment'))
638
os.write(fd, 'set visible to true\n')
639
os.write(fd, 'end tell\n')
640
os.write(fd, 'end tell\n')
642
os.close(fd) # Just close the handle but do not remove the file.
643
return [self.temp_file]
556
"""See ExternalMailClient._get_compose_commandline"""
558
fd, self.temp_file = tempfile.mkstemp(prefix="bzr-send-",
561
os.write(fd, 'tell application "Mail"\n')
562
os.write(fd, 'set newMessage to make new outgoing message\n')
563
os.write(fd, 'tell newMessage\n')
565
os.write(fd, 'make new to recipient with properties'
566
' {address:"%s"}\n' % to)
567
if from_ is not None:
568
# though from_ doesn't actually seem to be used
569
os.write(fd, 'set sender to "%s"\n'
570
% sender.replace('"', '\\"'))
571
if subject is not None:
572
os.write(fd, 'set subject to "%s"\n'
573
% subject.replace('"', '\\"'))
575
# FIXME: would be nice to prepend the body to the
576
# existing content (e.g., preserve signature), but
577
# can't seem to figure out the right applescript
579
os.write(fd, 'set content to "%s\\n\n"\n' %
580
body.replace('"', '\\"').replace('\n', '\\n'))
582
if attach_path is not None:
583
# FIXME: would be nice to first append a newline to
584
# ensure the attachment is on a new paragraph, but
585
# can't seem to figure out the right applescript
587
os.write(fd, 'tell content to make new attachment'
588
' with properties {file name:"%s"}'
589
' at after the last paragraph\n'
590
% self._encode_path(attach_path, 'attachment'))
591
os.write(fd, 'set visible to true\n')
592
os.write(fd, 'end tell\n')
593
os.write(fd, 'end tell\n')
595
os.close(fd) # Just close the handle but do not remove the file.
596
return [self.temp_file]
646
597
mail_client_registry.register('mail.app', MailApp,
647
598
help=MailApp.__doc__)
665
616
"""See MailClient.compose"""
667
618
return self._mail_client().compose(prompt, to, subject,
668
attachment, mime_subtype,
619
attachment, mimie_subtype,
669
620
extension, basename, body)
670
except MailClientNotFound:
671
return Editor(self.config).compose(
672
prompt, to, subject, attachment, mime_subtype, extension, body)
621
except errors.MailClientNotFound:
622
return Editor(self.config).compose(prompt, to, subject,
623
attachment, mimie_subtype, extension, body)
674
625
def compose_merge_request(self, to, subject, directive, basename=None,
676
627
"""See MailClient.compose_merge_request"""
678
return self._mail_client().compose_merge_request(
679
to, subject, directive, basename=basename, body=body)
680
except MailClientNotFound:
681
return Editor(self.config).compose_merge_request(
682
to, subject, directive, basename=basename, body=body)
685
mail_client_registry.register(u'default', DefaultMail,
629
return self._mail_client().compose_merge_request(to, subject,
630
directive, basename=basename, body=body)
631
except errors.MailClientNotFound:
632
return Editor(self.config).compose_merge_request(to, subject,
633
directive, basename=basename, body=body)
634
mail_client_registry.register('default', DefaultMail,
686
635
help=DefaultMail.__doc__)
687
mail_client_registry.default_key = u'default'
689
opt_mail_client = _mod_config.RegistryOption(
690
'mail_client', mail_client_registry, help='E-mail client to use.',
636
mail_client_registry.default_key = 'default'