diff options
Diffstat (limited to 'mercurial/mail.py')
-rw-r--r-- | mercurial/mail.py | 52 |
1 files changed, 15 insertions, 37 deletions
diff --git a/mercurial/mail.py b/mercurial/mail.py index 1154a4a..6f11ead 100644 --- a/mercurial/mail.py +++ b/mercurial/mail.py @@ -7,7 +7,7 @@ from i18n import _ import util, encoding -import os, smtplib, socket, quopri, time +import os, smtplib, socket, quopri import email.Header, email.MIMEText, email.Utils _oldheaderinit = email.Header.Header.__init__ @@ -37,7 +37,7 @@ def _smtp(ui): # backward compatible: when tls = true, we use starttls. starttls = tls == 'starttls' or util.parsebool(tls) smtps = tls == 'smtps' - if (starttls or smtps) and not util.safehasattr(socket, 'ssl'): + if (starttls or smtps) and not hasattr(socket, 'ssl'): raise util.Abort(_("can't use TLS: Python SSL support not installed")) if smtps: ui.note(_('(using smtps)\n')) @@ -93,29 +93,15 @@ def _sendmail(ui, sender, recipients, msg): os.path.basename(program.split(None, 1)[0]), util.explainexit(ret)[0])) -def _mbox(mbox, sender, recipients, msg): - '''write mails to mbox''' - fp = open(mbox, 'ab+') - # Should be time.asctime(), but Windows prints 2-characters day - # of month instead of one. Make them print the same thing. - date = time.strftime('%a %b %d %H:%M:%S %Y', time.localtime()) - fp.write('From %s %s\n' % (sender, date)) - fp.write(msg) - fp.write('\n\n') - fp.close() - -def connect(ui, mbox=None): +def connect(ui): '''make a mail connection. return a function to send mail. call as sendmail(sender, list-of-recipients, msg).''' - if mbox: - open(mbox, 'wb').close() - return lambda s, r, m: _mbox(mbox, s, r, m) if ui.config('email', 'method', 'smtp') == 'smtp': return _smtp(ui) return lambda s, r, m: _sendmail(ui, s, r, m) -def sendmail(ui, sender, recipients, msg, mbox=None): - send = connect(ui, mbox=mbox) +def sendmail(ui, sender, recipients, msg): + send = connect(ui) return send(sender, recipients, msg) def validateconfig(ui): @@ -131,9 +117,14 @@ def validateconfig(ui): 'but not in PATH') % method) def mimetextpatch(s, subtype='plain', display=False): - '''Return MIME message suitable for a patch. - Charset will be detected as utf-8 or (possibly fake) us-ascii. - Transfer encodings will be used if necessary.''' + '''If patch in utf-8 transfer-encode it.''' + + enc = None + for line in s.splitlines(): + if len(line) > 950: + s = quopri.encodestring(s) + enc = "quoted-printable" + break cs = 'us-ascii' if not display: @@ -147,20 +138,7 @@ def mimetextpatch(s, subtype='plain', display=False): # We'll go with us-ascii as a fallback. pass - return mimetextqp(s, subtype, cs) - -def mimetextqp(body, subtype, charset): - '''Return MIME message. - Qouted-printable transfer encoding will be used if necessary. - ''' - enc = None - for line in body.splitlines(): - if len(line) > 950: - body = quopri.encodestring(body) - enc = "quoted-printable" - break - - msg = email.MIMEText.MIMEText(body, subtype, charset) + msg = email.MIMEText.MIMEText(s, subtype, cs) if enc: del msg['Content-Transfer-Encoding'] msg['Content-Transfer-Encoding'] = enc @@ -252,4 +230,4 @@ def mimeencode(ui, s, charsets=None, display=False): cs = 'us-ascii' if not display: s, cs = _encode(ui, s, charsets) - return mimetextqp(s, 'plain', cs) + return email.MIMEText.MIMEText(s, 'plain', cs) |