##// END OF EJS Templates
allow to send email using sendmail....
Vadim Gelfer -
r2292:903ab41a default
parent child Browse files
Show More
@@ -135,6 +135,20 b' email::'
135 from;;
135 from;;
136 Optional. Email address to use in "From" header and SMTP envelope
136 Optional. Email address to use in "From" header and SMTP envelope
137 of outgoing messages.
137 of outgoing messages.
138 method;;
139 Optional. Method to use to send email messages. If value is
140 "smtp" (default), use SMTP (see section "[mail]" for
141 configuration). Otherwise, use as name of program to run that
142 acts like sendmail (takes "-f" option for sender, list of
143 recipients on command line, message on stdin). Normally, setting
144 this to "sendmail" or "/usr/sbin/sendmail" is enough to use
145 sendmail to send messages.
146
147 Email example:
148
149 [email]
150 from = Joseph User <joe.user@example.com>
151 method = /usr/sbin/sendmail
138
152
139 extensions::
153 extensions::
140 Mercurial has an extension mechanism for adding new features. To
154 Mercurial has an extension mechanism for adding new features. To
@@ -254,8 +254,6 b' def patchbomb(ui, repo, *revs, **opts):'
254 else:
254 else:
255 ui.status('Sending ', m['Subject'], ' ...\n')
255 ui.status('Sending ', m['Subject'], ' ...\n')
256 mail.sendmail(sender, to + cc, m.as_string(0))
256 mail.sendmail(sender, to + cc, m.as_string(0))
257 if not opts['test'] and not opts['mbox']:
258 mail.close()
259
257
260 cmdtable = {
258 cmdtable = {
261 'email':
259 'email':
@@ -8,7 +8,8 b''
8 import ConfigParser
8 import ConfigParser
9 from i18n import gettext as _
9 from i18n import gettext as _
10 from demandload import *
10 from demandload import *
11 demandload(globals(), "errno getpass os re smtplib socket sys tempfile util")
11 demandload(globals(), "errno getpass os re smtplib socket sys tempfile")
12 demandload(globals(), "templater util")
12
13
13 class ui(object):
14 class ui(object):
14 def __init__(self, verbose=False, debug=False, quiet=False,
15 def __init__(self, verbose=False, debug=False, quiet=False,
@@ -270,17 +271,56 b' class ui(object):'
270 return t
271 return t
271
272
272 def sendmail(self):
273 def sendmail(self):
273 s = smtplib.SMTP()
274 '''send mail message. object returned has one method, sendmail.
274 mailhost = self.config('smtp', 'host')
275 call as sendmail(sender, list-of-recipients, msg).'''
275 if not mailhost:
276
276 raise util.Abort(_('no [smtp]host in hgrc - cannot send mail'))
277 def smtp():
277 s.connect(host=mailhost, port=int(self.config('smtp', 'port', 25)))
278 '''send mail using smtp.'''
278 if self.configbool('smtp', 'tls'):
279
279 s.ehlo()
280 s = smtplib.SMTP()
280 s.starttls()
281 mailhost = self.config('smtp', 'host')
281 s.ehlo()
282 if not mailhost:
282 username = self.config('smtp', 'username')
283 raise util.Abort(_('no [smtp]host in hgrc - cannot send mail'))
283 password = self.config('smtp', 'password')
284 mailport = int(self.config('smtp', 'port', 25))
284 if username and password:
285 self.note(_('sending mail: smtp host %s, port %s\n') %
285 s.login(username, password)
286 (mailhost, mailport))
286 return s
287 s.connect(host=mailhost, port=mailport)
288 if self.configbool('smtp', 'tls'):
289 self.note(_('(using tls)\n'))
290 s.ehlo()
291 s.starttls()
292 s.ehlo()
293 username = self.config('smtp', 'username')
294 password = self.config('smtp', 'password')
295 if username and password:
296 self.note(_('(authenticating to mail server as %s)\n') %
297 (username))
298 s.login(username, password)
299 return s
300
301 class sendmail(object):
302 '''send mail using sendmail.'''
303
304 def __init__(self, ui, program):
305 self.ui = ui
306 self.program = program
307
308 def sendmail(self, sender, recipients, msg):
309 cmdline = '%s -f %s %s' % (
310 self.program, templater.email(sender),
311 ' '.join(map(templater.email, recipients)))
312 self.ui.note(_('sending mail: %s\n') % cmdline)
313 fp = os.popen(cmdline, 'w')
314 fp.write(msg)
315 ret = fp.close()
316 if ret:
317 raise util.Abort('%s %s' % (
318 os.path.basename(self.program.split(None, 1)[0]),
319 util.explain_exit(ret)[0]))
320
321 method = self.config('email', 'method', 'smtp')
322 if method == 'smtp':
323 mail = smtp()
324 else:
325 mail = sendmail(self, method)
326 return mail
General Comments 0
You need to be logged in to leave comments. Login now