Mercurial > public > mercurial-scm > hg-stable
diff mercurial/mail.py @ 8332:3e544c074459
patchbomb: quoted-printable encode overly long lines
RfC2822 mandates a line length limit of 998 byte + CRLF.
Python mail tools break lines at 990 byte. To prevent
that, we quoted-printable encode overly long lines.
author | Rocco Rutte <pdmef@gmx.net> |
---|---|
date | Fri, 08 May 2009 18:30:44 +0200 |
parents | b87a50b7125c |
children | 6fbbb90261b1 |
line wrap: on
line diff
--- a/mercurial/mail.py Sat May 09 01:15:24 2009 +0200 +++ b/mercurial/mail.py Fri May 08 18:30:44 2009 +0200 @@ -7,7 +7,7 @@ from i18n import _ import util, encoding -import os, smtplib, socket +import os, smtplib, socket, quopri import email.Header, email.MIMEText, email.Utils def _smtp(ui): @@ -88,14 +88,31 @@ def mimetextpatch(s, subtype='plain', display=False): '''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: - for cs in ('us-ascii', 'utf-8'): + try: + s.decode('us-ascii') + except UnicodeDecodeError: try: - s.decode(cs) - return email.MIMEText.MIMEText(s, subtype, cs) + s.decode('utf-8') + cs = 'utf-8' except UnicodeDecodeError: + # We'll go with us-ascii as a fallback. pass - return email.MIMEText.MIMEText(s, subtype) + + msg = email.MIMEText.MIMEText(s, subtype, cs) + if enc: + del msg['Content-Transfer-Encoding'] + msg['Content-Transfer-Encoding'] = enc + return msg def _charsets(ui): '''Obtains charsets to send mail parts not containing patches.'''