Mercurial > public > mercurial-scm > hg
comparison mercurial/mail.py @ 5866:dc6ed2736c81
patchbomb: prompt only once for SMTP password
- simplify mail._sendmail to be a function rather than a class
- simplify connect to return a function rather than a class
- move exception handling from mail.sendmail to mail.connect
- use a single connection for all messages in patchbomb
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Thu, 17 Jan 2008 13:51:59 -0600 |
parents | 4fba4fee0718 |
children | 528c986f0162 |
comparison
equal
deleted
inserted
replaced
5865:e7127f669edb | 5866:dc6ed2736c81 |
---|---|
36 ui.note(_('(authenticating to mail server as %s)\n') % | 36 ui.note(_('(authenticating to mail server as %s)\n') % |
37 (username)) | 37 (username)) |
38 s.login(username, password) | 38 s.login(username, password) |
39 return s | 39 return s |
40 | 40 |
41 class _sendmail(object): | 41 def _sendmail(ui, sender, recipients, msg): |
42 '''send mail using sendmail.''' | 42 '''send mail using sendmail.''' |
43 | 43 program = ui.config('email', 'method') |
44 def __init__(self, ui, program): | 44 cmdline = '%s -f %s %s' % (program, templater.email(sender), |
45 self.ui = ui | 45 ' '.join(map(templater.email, recipients))) |
46 self.program = program | 46 ui.note(_('sending mail: %s\n') % cmdline) |
47 | 47 fp = os.popen(cmdline, 'w') |
48 def sendmail(self, sender, recipients, msg): | 48 fp.write(msg) |
49 cmdline = '%s -f %s %s' % ( | 49 ret = fp.close() |
50 self.program, templater.email(sender), | 50 if ret: |
51 ' '.join(map(templater.email, recipients))) | 51 raise util.Abort('%s %s' % ( |
52 self.ui.note(_('sending mail: %s\n') % cmdline) | 52 os.path.basename(program.split(None, 1)[0]), |
53 fp = os.popen(cmdline, 'w') | 53 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 | 54 |
61 def connect(ui): | 55 def connect(ui): |
62 '''make a mail connection. object returned has one method, sendmail. | 56 '''make a mail connection. return a function to send mail. |
63 call as sendmail(sender, list-of-recipients, msg).''' | 57 call as sendmail(sender, list-of-recipients, msg).''' |
64 | 58 |
65 method = ui.config('email', 'method', 'smtp') | 59 func = _sendmail |
66 if method == 'smtp': | 60 if ui.config('email', 'method', 'smtp') == 'smtp': |
67 return _smtp(ui) | 61 func = _smtp(ui) |
68 | 62 |
69 return _sendmail(ui, method) | 63 def send(ui, sender, recipients, msg): |
64 try: | |
65 return func.sendmail(sender, recipients, msg) | |
66 except smtplib.SMTPRecipientsRefused, inst: | |
67 recipients = [r[1] for r in inst.recipients.values()] | |
68 raise util.Abort('\n' + '\n'.join(recipients)) | |
69 except smtplib.SMTPException, inst: | |
70 raise util.Abort(inst) | |
71 | |
72 return send | |
70 | 73 |
71 def sendmail(ui, sender, recipients, msg): | 74 def sendmail(ui, sender, recipients, msg): |
72 try: | 75 try: |
73 return connect(ui).sendmail(sender, recipients, msg) | 76 send = connect(ui) |
77 return send(sender, recipients, msg) | |
74 except smtplib.SMTPRecipientsRefused, inst: | 78 except smtplib.SMTPRecipientsRefused, inst: |
75 recipients = [r[1] for r in inst.recipients.values()] | 79 recipients = [r[1] for r in inst.recipients.values()] |
76 raise util.Abort('\n' + '\n'.join(recipients)) | 80 raise util.Abort('\n' + '\n'.join(recipients)) |
77 except smtplib.SMTPException, inst: | 81 except smtplib.SMTPException, inst: |
78 raise util.Abort(inst) | 82 raise util.Abort(inst) |