comparison mercurial/mail.py @ 15560:cc58c228503e

mail: mbox handling as a part of mail handling, refactored from patchbomb
author Mads Kiilerich <mads@kiilerich.com>
date Wed, 23 Nov 2011 02:11:24 +0100
parents 194b043dfa51
children ca572e94d8e7
comparison
equal deleted inserted replaced
15559:1830d0cc4bc1 15560:cc58c228503e
5 # This software may be used and distributed according to the terms of the 5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version. 6 # GNU General Public License version 2 or any later version.
7 7
8 from i18n import _ 8 from i18n import _
9 import util, encoding 9 import util, encoding
10 import os, smtplib, socket, quopri 10 import os, smtplib, socket, quopri, time
11 import email.Header, email.MIMEText, email.Utils 11 import email.Header, email.MIMEText, email.Utils
12 12
13 _oldheaderinit = email.Header.Header.__init__ 13 _oldheaderinit = email.Header.Header.__init__
14 def _unifiedheaderinit(self, *args, **kw): 14 def _unifiedheaderinit(self, *args, **kw):
15 """ 15 """
91 if ret: 91 if ret:
92 raise util.Abort('%s %s' % ( 92 raise util.Abort('%s %s' % (
93 os.path.basename(program.split(None, 1)[0]), 93 os.path.basename(program.split(None, 1)[0]),
94 util.explainexit(ret)[0])) 94 util.explainexit(ret)[0]))
95 95
96 def connect(ui): 96 def _mbox(mbox, sender, recipients, msg):
97 '''write mails to mbox'''
98 fp = open(mbox, 'ab+')
99 # Should be time.asctime(), but Windows prints 2-characters day
100 # of month instead of one. Make them print the same thing.
101 date = time.strftime('%a %b %d %H:%M:%S %Y', time.localtime())
102 fp.write('From %s %s\n' % (sender, date))
103 fp.write(msg)
104 fp.write('\n\n')
105 fp.close()
106
107 def connect(ui, mbox=None):
97 '''make a mail connection. return a function to send mail. 108 '''make a mail connection. return a function to send mail.
98 call as sendmail(sender, list-of-recipients, msg).''' 109 call as sendmail(sender, list-of-recipients, msg).'''
110 if mbox:
111 open(mbox, 'wb').close()
112 return lambda s, r, m: _mbox(mbox, s, r, m)
99 if ui.config('email', 'method', 'smtp') == 'smtp': 113 if ui.config('email', 'method', 'smtp') == 'smtp':
100 return _smtp(ui) 114 return _smtp(ui)
101 return lambda s, r, m: _sendmail(ui, s, r, m) 115 return lambda s, r, m: _sendmail(ui, s, r, m)
102 116
103 def sendmail(ui, sender, recipients, msg): 117 def sendmail(ui, sender, recipients, msg):