##// END OF EJS Templates
mail: fix connect
Brendan Cully -
r2929:bd748985 default
parent child Browse files
Show More
@@ -1,68 +1,68 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")
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 self.note(_('sending mail: smtp host %s, port %s\n') %
21 self.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 ui.note(_('(using tls)\n'))
25 ui.note(_('(using tls)\n'))
26 s.ehlo()
26 s.ehlo()
27 s.starttls()
27 s.starttls()
28 s.ehlo()
28 s.ehlo()
29 username = ui.config('smtp', 'username')
29 username = ui.config('smtp', 'username')
30 password = ui.config('smtp', 'password')
30 password = ui.config('smtp', 'password')
31 if username and password:
31 if username and password:
32 ui.note(_('(authenticating to mail server as %s)\n') %
32 ui.note(_('(authenticating to mail server as %s)\n') %
33 (username))
33 (username))
34 s.login(username, password)
34 s.login(username, password)
35 return s
35 return s
36
36
37 class _sendmail(object):
37 class _sendmail(object):
38 '''send mail using sendmail.'''
38 '''send mail using sendmail.'''
39
39
40 def __init__(self, ui, program):
40 def __init__(self, ui, program):
41 self.ui = ui
41 self.ui = ui
42 self.program = program
42 self.program = program
43
43
44 def sendmail(self, sender, recipients, msg):
44 def sendmail(self, sender, recipients, msg):
45 cmdline = '%s -f %s %s' % (
45 cmdline = '%s -f %s %s' % (
46 self.program, templater.email(sender),
46 self.program, templater.email(sender),
47 ' '.join(map(templater.email, recipients)))
47 ' '.join(map(templater.email, recipients)))
48 self.ui.note(_('sending mail: %s\n') % cmdline)
48 self.ui.note(_('sending mail: %s\n') % cmdline)
49 fp = os.popen(cmdline, 'w')
49 fp = os.popen(cmdline, 'w')
50 fp.write(msg)
50 fp.write(msg)
51 ret = fp.close()
51 ret = fp.close()
52 if ret:
52 if ret:
53 raise util.Abort('%s %s' % (
53 raise util.Abort('%s %s' % (
54 os.path.basename(self.program.split(None, 1)[0]),
54 os.path.basename(self.program.split(None, 1)[0]),
55 util.explain_exit(ret)[0]))
55 util.explain_exit(ret)[0]))
56
56
57 def connect(ui):
57 def connect(ui):
58 '''make a mail connection. object returned has one method, sendmail.
58 '''make a mail connection. object returned has one method, sendmail.
59 call as sendmail(sender, list-of-recipients, msg).'''
59 call as sendmail(sender, list-of-recipients, msg).'''
60
60
61 method = ui.config('email', 'method', 'smtp')
61 method = ui.config('email', 'method', 'smtp')
62 if method == 'smtp':
62 if method == 'smtp':
63 return smtp(ui)
63 return _smtp(ui)
64
64
65 return sendmail(ui, method)
65 return _sendmail(ui, method)
66
66
67 def sendmail(ui, sender, recipients, msg):
67 def sendmail(ui, sender, recipients, msg):
68 return connect(ui).sendmail(sender, recipients, msg)
68 return connect(ui).sendmail(sender, recipients, msg)
General Comments 0
You need to be logged in to leave comments. Login now