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(), "errno getpass os re smtplib socket sys tempfile") |
10 demandload(globals(), "errno getpass os re socket sys tempfile") |
11 demandload(globals(), "ConfigParser mdiff templater traceback util") |
11 demandload(globals(), "ConfigParser mdiff templater traceback util") |
12 |
12 |
13 class ui(object): |
13 class ui(object): |
14 def __init__(self, verbose=False, debug=False, quiet=False, |
14 def __init__(self, verbose=False, debug=False, quiet=False, |
15 interactive=True, traceback=False, parentui=None): |
15 interactive=True, traceback=False, parentui=None): |
279 finally: |
279 finally: |
280 os.unlink(name) |
280 os.unlink(name) |
281 |
281 |
282 return t |
282 return t |
283 |
283 |
284 def sendmail(self): |
|
285 '''send mail message. object returned has one method, sendmail. |
|
286 call as sendmail(sender, list-of-recipients, msg).''' |
|
287 |
|
288 def smtp(): |
|
289 '''send mail using smtp.''' |
|
290 |
|
291 local_hostname = self.config('smtp', 'local_hostname') |
|
292 s = smtplib.SMTP(local_hostname=local_hostname) |
|
293 mailhost = self.config('smtp', 'host') |
|
294 if not mailhost: |
|
295 raise util.Abort(_('no [smtp]host in hgrc - cannot send mail')) |
|
296 mailport = int(self.config('smtp', 'port', 25)) |
|
297 self.note(_('sending mail: smtp host %s, port %s\n') % |
|
298 (mailhost, mailport)) |
|
299 s.connect(host=mailhost, port=mailport) |
|
300 if self.configbool('smtp', 'tls'): |
|
301 self.note(_('(using tls)\n')) |
|
302 s.ehlo() |
|
303 s.starttls() |
|
304 s.ehlo() |
|
305 username = self.config('smtp', 'username') |
|
306 password = self.config('smtp', 'password') |
|
307 if username and password: |
|
308 self.note(_('(authenticating to mail server as %s)\n') % |
|
309 (username)) |
|
310 s.login(username, password) |
|
311 return s |
|
312 |
|
313 class sendmail(object): |
|
314 '''send mail using sendmail.''' |
|
315 |
|
316 def __init__(self, ui, program): |
|
317 self.ui = ui |
|
318 self.program = program |
|
319 |
|
320 def sendmail(self, sender, recipients, msg): |
|
321 cmdline = '%s -f %s %s' % ( |
|
322 self.program, templater.email(sender), |
|
323 ' '.join(map(templater.email, recipients))) |
|
324 self.ui.note(_('sending mail: %s\n') % cmdline) |
|
325 fp = os.popen(cmdline, 'w') |
|
326 fp.write(msg) |
|
327 ret = fp.close() |
|
328 if ret: |
|
329 raise util.Abort('%s %s' % ( |
|
330 os.path.basename(self.program.split(None, 1)[0]), |
|
331 util.explain_exit(ret)[0])) |
|
332 |
|
333 method = self.config('email', 'method', 'smtp') |
|
334 if method == 'smtp': |
|
335 mail = smtp() |
|
336 else: |
|
337 mail = sendmail(self, method) |
|
338 return mail |
|
339 |
|
340 def print_exc(self): |
284 def print_exc(self): |
341 '''print exception traceback if traceback printing enabled. |
285 '''print exception traceback if traceback printing enabled. |
342 only to call in exception handler. returns true if traceback |
286 only to call in exception handler. returns true if traceback |
343 printed.''' |
287 printed.''' |
344 if self.traceback: |
288 if self.traceback: |