comparison mercurial/mail.py @ 34310:2d0c306a88c2

mail: encode long unicode lines in emails properly (issue5687) 3e544c074459 introduced a bug: emails Content-Transfer-Encoding is silently replaced with 'quoted-printable' while any other encoding could be used by underlying code. The problem is revealed when a long unicode line is encoded. The patch implements proper check which works for any text and encoding.
author Igor Ippolitov <iippolitov@gmail.com>
date Tue, 26 Sep 2017 16:14:57 +0300
parents 0407a51b9d8c
children b45a9d121b53
comparison
equal deleted inserted replaced
34309:b94db1780365 34310:2d0c306a88c2
8 from __future__ import absolute_import 8 from __future__ import absolute_import
9 9
10 import email 10 import email
11 import email.charset 11 import email.charset
12 import email.header 12 import email.header
13 import email.message
13 import os 14 import os
14 import quopri
15 import smtplib 15 import smtplib
16 import socket 16 import socket
17 import time 17 import time
18 18
19 from .i18n import _ 19 from .i18n import _
214 214
215 def mimetextqp(body, subtype, charset): 215 def mimetextqp(body, subtype, charset):
216 '''Return MIME message. 216 '''Return MIME message.
217 Quoted-printable transfer encoding will be used if necessary. 217 Quoted-printable transfer encoding will be used if necessary.
218 ''' 218 '''
219 enc = None 219 cs = email.charset.Charset(charset)
220 msg = email.message.Message()
221 msg.set_type('text/' + subtype)
222
220 for line in body.splitlines(): 223 for line in body.splitlines():
221 if len(line) > 950: 224 if len(line) > 950:
222 body = quopri.encodestring(body) 225 cs.body_encoding = email.charset.QP
223 enc = "quoted-printable"
224 break 226 break
225 227
226 msg = email.MIMEText.MIMEText(body, subtype, charset) 228 msg.set_payload(body, cs)
227 if enc: 229
228 del msg['Content-Transfer-Encoding']
229 msg['Content-Transfer-Encoding'] = enc
230 return msg 230 return msg
231 231
232 def _charsets(ui): 232 def _charsets(ui):
233 '''Obtains charsets to send mail parts not containing patches.''' 233 '''Obtains charsets to send mail parts not containing patches.'''
234 charsets = [cs.lower() for cs in ui.configlist('email', 'charsets')] 234 charsets = [cs.lower() for cs in ui.configlist('email', 'charsets')]