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