7 |
7 |
8 from i18n import _ |
8 from i18n import _ |
9 import os, smtplib, templater, util, socket |
9 import os, smtplib, templater, util, socket |
10 |
10 |
11 def _smtp(ui): |
11 def _smtp(ui): |
12 '''send mail using smtp.''' |
12 '''build an smtp connection and return a function to send mail''' |
13 |
|
14 local_hostname = ui.config('smtp', 'local_hostname') |
13 local_hostname = ui.config('smtp', 'local_hostname') |
15 s = smtplib.SMTP(local_hostname=local_hostname) |
14 s = smtplib.SMTP(local_hostname=local_hostname) |
16 mailhost = ui.config('smtp', 'host') |
15 mailhost = ui.config('smtp', 'host') |
17 if not mailhost: |
16 if not mailhost: |
18 raise util.Abort(_('no [smtp]host in hgrc - cannot send mail')) |
17 raise util.Abort(_('no [smtp]host in hgrc - cannot send mail')) |
34 password = ui.getpass() |
33 password = ui.getpass() |
35 if username and password: |
34 if username and password: |
36 ui.note(_('(authenticating to mail server as %s)\n') % |
35 ui.note(_('(authenticating to mail server as %s)\n') % |
37 (username)) |
36 (username)) |
38 s.login(username, password) |
37 s.login(username, password) |
39 return s |
|
40 |
38 |
41 class _sendmail(object): |
39 def send(sender, recipients, msg): |
|
40 try: |
|
41 return s.sendmail(sender, recipients, msg) |
|
42 except smtplib.SMTPRecipientsRefused, inst: |
|
43 recipients = [r[1] for r in inst.recipients.values()] |
|
44 raise util.Abort('\n' + '\n'.join(recipients)) |
|
45 except smtplib.SMTPException, inst: |
|
46 raise util.Abort(inst) |
|
47 |
|
48 return send |
|
49 |
|
50 def _sendmail(ui, sender, recipients, msg): |
42 '''send mail using sendmail.''' |
51 '''send mail using sendmail.''' |
43 |
52 program = ui.config('email', 'method') |
44 def __init__(self, ui, program): |
53 cmdline = '%s -f %s %s' % (program, templater.email(sender), |
45 self.ui = ui |
54 ' '.join(map(templater.email, recipients))) |
46 self.program = program |
55 ui.note(_('sending mail: %s\n') % cmdline) |
47 |
56 fp = os.popen(cmdline, 'w') |
48 def sendmail(self, sender, recipients, msg): |
57 fp.write(msg) |
49 cmdline = '%s -f %s %s' % ( |
58 ret = fp.close() |
50 self.program, templater.email(sender), |
59 if ret: |
51 ' '.join(map(templater.email, recipients))) |
60 raise util.Abort('%s %s' % ( |
52 self.ui.note(_('sending mail: %s\n') % cmdline) |
61 os.path.basename(program.split(None, 1)[0]), |
53 fp = os.popen(cmdline, 'w') |
62 util.explain_exit(ret)[0])) |
54 fp.write(msg) |
|
55 ret = fp.close() |
|
56 if ret: |
|
57 raise util.Abort('%s %s' % ( |
|
58 os.path.basename(self.program.split(None, 1)[0]), |
|
59 util.explain_exit(ret)[0])) |
|
60 |
63 |
61 def connect(ui): |
64 def connect(ui): |
62 '''make a mail connection. object returned has one method, sendmail. |
65 '''make a mail connection. return a function to send mail. |
63 call as sendmail(sender, list-of-recipients, msg).''' |
66 call as sendmail(sender, list-of-recipients, msg).''' |
64 |
67 if ui.config('email', 'method', 'smtp') == 'smtp': |
65 method = ui.config('email', 'method', 'smtp') |
|
66 if method == 'smtp': |
|
67 return _smtp(ui) |
68 return _smtp(ui) |
68 |
69 return lambda s, r, m: _sendmail(ui, s, r, m) |
69 return _sendmail(ui, method) |
|
70 |
70 |
71 def sendmail(ui, sender, recipients, msg): |
71 def sendmail(ui, sender, recipients, msg): |
72 try: |
72 send = connect(ui) |
73 return connect(ui).sendmail(sender, recipients, msg) |
73 return send(sender, recipients, msg) |
74 except smtplib.SMTPRecipientsRefused, inst: |
|
75 recipients = [r[1] for r in inst.recipients.values()] |
|
76 raise util.Abort('\n' + '\n'.join(recipients)) |
|
77 except smtplib.SMTPException, inst: |
|
78 raise util.Abort(inst) |
|
79 |
74 |
80 def validateconfig(ui): |
75 def validateconfig(ui): |
81 '''determine if we have enough config data to try sending email.''' |
76 '''determine if we have enough config data to try sending email.''' |
82 method = ui.config('email', 'method', 'smtp') |
77 method = ui.config('email', 'method', 'smtp') |
83 if method == 'smtp': |
78 if method == 'smtp': |