Mercurial > public > mercurial-scm > hg
comparison mercurial/mail.py @ 15562:a82b6038ff08
mail: use quoted-printable for mime encoding to avoid too long lines (issue3075)
Quoted-printable was already used for the more critical patch mails, so it
should be fine for everything else as well.
author | Mads Kiilerich <mads@kiilerich.com> |
---|---|
date | Wed, 23 Nov 2011 02:44:11 +0100 |
parents | ca572e94d8e7 |
children | e7cfe3587ea4 |
comparison
equal
deleted
inserted
replaced
15561:ca572e94d8e7 | 15562:a82b6038ff08 |
---|---|
129 if not util.findexe(method): | 129 if not util.findexe(method): |
130 raise util.Abort(_('%r specified as email transport, ' | 130 raise util.Abort(_('%r specified as email transport, ' |
131 'but not in PATH') % method) | 131 'but not in PATH') % method) |
132 | 132 |
133 def mimetextpatch(s, subtype='plain', display=False): | 133 def mimetextpatch(s, subtype='plain', display=False): |
134 '''If patch in utf-8 transfer-encode it.''' | 134 '''Return MIME message suitable for a patch. |
135 | 135 Charset will be detected as utf-8 or (possibly fake) us-ascii. |
136 enc = None | 136 Transfer encodings will be used if necessary.''' |
137 for line in s.splitlines(): | |
138 if len(line) > 950: | |
139 s = quopri.encodestring(s) | |
140 enc = "quoted-printable" | |
141 break | |
142 | 137 |
143 cs = 'us-ascii' | 138 cs = 'us-ascii' |
144 if not display: | 139 if not display: |
145 try: | 140 try: |
146 s.decode('us-ascii') | 141 s.decode('us-ascii') |
150 cs = 'utf-8' | 145 cs = 'utf-8' |
151 except UnicodeDecodeError: | 146 except UnicodeDecodeError: |
152 # We'll go with us-ascii as a fallback. | 147 # We'll go with us-ascii as a fallback. |
153 pass | 148 pass |
154 | 149 |
155 msg = email.MIMEText.MIMEText(s, subtype, cs) | 150 return mimetextqp(s, subtype, cs) |
151 | |
152 def mimetextqp(body, subtype, charset): | |
153 '''Return MIME message. | |
154 Qouted-printable transfer encoding will be used if necessary. | |
155 ''' | |
156 enc = None | |
157 for line in body.splitlines(): | |
158 if len(line) > 950: | |
159 body = quopri.encodestring(body) | |
160 enc = "quoted-printable" | |
161 break | |
162 | |
163 msg = email.MIMEText.MIMEText(body, subtype, charset) | |
156 if enc: | 164 if enc: |
157 del msg['Content-Transfer-Encoding'] | 165 del msg['Content-Transfer-Encoding'] |
158 msg['Content-Transfer-Encoding'] = enc | 166 msg['Content-Transfer-Encoding'] = enc |
159 return msg | 167 return msg |
160 | 168 |
242 '''creates mime text object, encodes it if needed, and sets | 250 '''creates mime text object, encodes it if needed, and sets |
243 charset and transfer-encoding accordingly.''' | 251 charset and transfer-encoding accordingly.''' |
244 cs = 'us-ascii' | 252 cs = 'us-ascii' |
245 if not display: | 253 if not display: |
246 s, cs = _encode(ui, s, charsets) | 254 s, cs = _encode(ui, s, charsets) |
247 return email.MIMEText.MIMEText(s, 'plain', cs) | 255 return mimetextqp(s, 'plain', cs) |