##// END OF EJS Templates
mail: mbox handling as a part of mail handling, refactored from patchbomb
Mads Kiilerich -
r15560:cc58c228 default
parent child Browse files
Show More
@@ -45,7 +45,7 b' directly from the commandline. See the ['
45 hgrc(5) for details.
45 hgrc(5) for details.
46 '''
46 '''
47
47
48 import os, errno, socket, tempfile, cStringIO, time
48 import os, errno, socket, tempfile, cStringIO
49 import email.MIMEMultipart, email.MIMEBase
49 import email.MIMEMultipart, email.MIMEBase
50 import email.Utils, email.Encoders, email.Generator
50 import email.Utils, email.Encoders, email.Generator
51 from mercurial import cmdutil, commands, hg, mail, patch, util, discovery
51 from mercurial import cmdutil, commands, hg, mail, patch, util, discovery
@@ -532,25 +532,14 b' def patchbomb(ui, repo, *revs, **opts):'
532 raise
532 raise
533 if fp is not ui:
533 if fp is not ui:
534 fp.close()
534 fp.close()
535 elif mbox:
535 else:
536 if not sendmail:
537 sendmail = mail.connect(ui, mbox=mbox)
536 ui.status(_('Sending '), subj, ' ...\n')
538 ui.status(_('Sending '), subj, ' ...\n')
537 ui.progress(_('sending'), i, item=subj, total=len(msgs))
539 ui.progress(_('sending'), i, item=subj, total=len(msgs))
538 fp = open(mbox, i > 0 and 'ab+' or 'wb+')
540 if not mbox:
539 generator = email.Generator.Generator(fp, mangle_from_=True)
541 # Exim does not remove the Bcc field
540 # Should be time.asctime(), but Windows prints 2-characters day
542 del m['Bcc']
541 # of month instead of one. Make them print the same thing.
542 date = time.strftime('%a %b %d %H:%M:%S %Y', time.localtime())
543 fp.write('From %s %s\n' % (sender_addr, date))
544 generator.flatten(m, 0)
545 fp.write('\n\n')
546 fp.close()
547 else:
548 if not sendmail:
549 sendmail = mail.connect(ui)
550 ui.status(_('Sending '), subj, ' ...\n')
551 ui.progress(_('sending'), i, item=subj, total=len(msgs))
552 # Exim does not remove the Bcc field
553 del m['Bcc']
554 fp = cStringIO.StringIO()
543 fp = cStringIO.StringIO()
555 generator = email.Generator.Generator(fp, mangle_from_=False)
544 generator = email.Generator.Generator(fp, mangle_from_=False)
556 generator.flatten(m, 0)
545 generator.flatten(m, 0)
@@ -7,7 +7,7 b''
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__
@@ -93,9 +93,23 b' def _sendmail(ui, sender, recipients, ms'
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)
General Comments 0
You need to be logged in to leave comments. Login now