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