##// END OF EJS Templates
mail.py: don't try to use TLS if python doesn't have SSL support...
Alexis S. L. Carvalho -
r4093:669f99f7 default
parent child Browse files
Show More
@@ -1,68 +1,71 b''
1 1 # mail.py - mail sending bits for mercurial
2 2 #
3 3 # Copyright 2006 Matt Mackall <mpm@selenic.com>
4 4 #
5 5 # This software may be used and distributed according to the terms
6 6 # of the GNU General Public License, incorporated herein by reference.
7 7
8 8 from i18n import gettext as _
9 9 from demandload import *
10 demandload(globals(), "os re smtplib templater util")
10 demandload(globals(), "os re smtplib templater util socket")
11 11
12 12 def _smtp(ui):
13 13 '''send mail using smtp.'''
14 14
15 15 local_hostname = ui.config('smtp', 'local_hostname')
16 16 s = smtplib.SMTP(local_hostname=local_hostname)
17 17 mailhost = ui.config('smtp', 'host')
18 18 if not mailhost:
19 19 raise util.Abort(_('no [smtp]host in hgrc - cannot send mail'))
20 20 mailport = int(ui.config('smtp', 'port', 25))
21 21 ui.note(_('sending mail: smtp host %s, port %s\n') %
22 22 (mailhost, mailport))
23 23 s.connect(host=mailhost, port=mailport)
24 24 if ui.configbool('smtp', 'tls'):
25 if not hasattr(socket, 'ssl'):
26 raise util.Abort(_("can't use TLS: Python SSL support "
27 "not installed"))
25 28 ui.note(_('(using tls)\n'))
26 29 s.ehlo()
27 30 s.starttls()
28 31 s.ehlo()
29 32 username = ui.config('smtp', 'username')
30 33 password = ui.config('smtp', 'password')
31 34 if username and password:
32 35 ui.note(_('(authenticating to mail server as %s)\n') %
33 36 (username))
34 37 s.login(username, password)
35 38 return s
36 39
37 40 class _sendmail(object):
38 41 '''send mail using sendmail.'''
39 42
40 43 def __init__(self, ui, program):
41 44 self.ui = ui
42 45 self.program = program
43 46
44 47 def sendmail(self, sender, recipients, msg):
45 48 cmdline = '%s -f %s %s' % (
46 49 self.program, templater.email(sender),
47 50 ' '.join(map(templater.email, recipients)))
48 51 self.ui.note(_('sending mail: %s\n') % cmdline)
49 52 fp = os.popen(cmdline, 'w')
50 53 fp.write(msg)
51 54 ret = fp.close()
52 55 if ret:
53 56 raise util.Abort('%s %s' % (
54 57 os.path.basename(self.program.split(None, 1)[0]),
55 58 util.explain_exit(ret)[0]))
56 59
57 60 def connect(ui):
58 61 '''make a mail connection. object returned has one method, sendmail.
59 62 call as sendmail(sender, list-of-recipients, msg).'''
60 63
61 64 method = ui.config('email', 'method', 'smtp')
62 65 if method == 'smtp':
63 66 return _smtp(ui)
64 67
65 68 return _sendmail(ui, method)
66 69
67 70 def sendmail(ui, sender, recipients, msg):
68 71 return connect(ui).sendmail(sender, recipients, msg)
General Comments 0
You need to be logged in to leave comments. Login now