Edgewall Software

Ticket #8252: 8252-8bit-body-encoding-r8165.patch

File 8252-8bit-body-encoding-r8165.patch, 4.4 KB (added by rblank, 3 years ago)

Patch against 0.11-stable using either 7bit or 8bit body encoding when mime_encoding = none.

  • trac/notification.py

    diff --git a/trac/notification.py b/trac/notification.py
    a b  
    7979        """Comma-separated list of domains that should be considered as 
    8080        valid for email addresses (such as localdomain)""") 
    8181            
    82     mime_encoding = Option('notification', 'mime_encoding', 'base64', 
     82    mime_encoding = Option('notification', 'mime_encoding', 'none', 
    8383        """Specifies the MIME encoding scheme for emails. 
    8484         
    8585        Valid options are 'base64' for Base64 encoding, 'qp' for 
    86         Quoted-Printable, and 'none' for no encoding. Note that the no encoding 
    87         means that non-ASCII characters in text are going to cause problems 
    88         with notifications (''since 0.10'').""") 
     86        Quoted-Printable, and 'none' for no encoding, in which case mails will 
     87        be sent as 7bit if the content is all ASCII, or 8bit otherwise. 
     88        (''since 0.10'')""") 
    8989 
    9090    use_public_cc = BoolOption('notification', 'use_public_cc', 'false', 
    9191        """Recipients can see email addresses of other CC'ed recipients. 
     
    196196                self.email_map[username] = email 
    197197                 
    198198    def _init_pref_encoding(self): 
    199         from email.Charset import Charset, QP, BASE64 
     199        from email.Charset import Charset, QP, BASE64, SHORTEST 
    200200        self._charset = Charset() 
    201201        self._charset.input_charset = 'utf-8' 
     202        self._charset.output_charset = 'utf-8' 
     203        self._charset.input_codec = 'utf-8' 
     204        self._charset.output_codec = 'utf-8' 
    202205        pref = self.env.config.get('notification', 'mime_encoding').lower() 
    203206        if pref == 'base64': 
    204207            self._charset.header_encoding = BASE64 
    205208            self._charset.body_encoding = BASE64 
    206             self._charset.output_charset = 'utf-8' 
    207             self._charset.input_codec = 'utf-8' 
    208             self._charset.output_codec = 'utf-8' 
    209209        elif pref in ['qp', 'quoted-printable']: 
    210210            self._charset.header_encoding = QP 
    211211            self._charset.body_encoding = QP 
    212             self._charset.output_charset = 'utf-8' 
    213             self._charset.input_codec = 'utf-8' 
    214             self._charset.output_codec = 'utf-8' 
    215212        elif pref == 'none': 
    216             self._charset.header_encoding = None 
     213            self._charset.header_encoding = SHORTEST 
    217214            self._charset.body_encoding = None 
    218             self._charset.input_codec = None 
    219             self._charset.output_charset = 'ascii' 
    220215        else: 
    221216            raise TracError(_('Invalid email encoding setting: %s' % pref)) 
    222217 
     
    388383        if pcc: 
    389384            headers['Cc'] = ', '.join(pcc) 
    390385        headers['Date'] = formatdate() 
    391         # sanity check 
    392         if not self._charset.body_encoding: 
    393             try: 
    394                 dummy = body.encode('ascii') 
    395             except UnicodeDecodeError: 
    396                 raise TracError(_("Ticket contains non-ASCII chars. " \ 
    397                                   "Please change encoding setting")) 
    398386        msg = MIMEText(body, 'plain') 
    399387        # Message class computes the wrong type from MIMEText constructor, 
    400388        # which does not take a Charset object as initializer. Reset the 
  • trac/ticket/tests/notification.py

    diff --git a/trac/ticket/tests/notification.py b/trac/ticket/tests/notification.py
    a b  
    411411        self._validate_mimebody((quopri, 'quoted-printable', 'utf-8'), 
    412412                                ticket, True) 
    413413 
    414     def test_mimebody_none(self): 
    415         """MIME None/ascii encoding""" 
     414    def test_mimebody_none_7bit(self): 
     415        """MIME None encoding resulting in 7bit""" 
    416416        self.env.config.set('notification','mime_encoding', 'none') 
    417417        ticket = Ticket(self.env) 
    418418        ticket['reporter'] = 'joe.user' 
    419419        ticket['summary'] = u'This is a summary' 
    420420        ticket.insert() 
    421         self._validate_mimebody((None, '7bit', 'ascii'), \ 
     421        self._validate_mimebody((None, '7bit', 'utf-8'), \ 
     422                                ticket, True) 
     423 
     424    def test_mimebody_none_8bit(self): 
     425        """MIME None encoding resulting in 8bit""" 
     426        self.env.config.set('notification','mime_encoding', 'none') 
     427        ticket = Ticket(self.env) 
     428        ticket['reporter'] = 'joe.user' 
     429        ticket['summary'] = u'This is a summary for Jöe Usèr' 
     430        ticket.insert() 
     431        self._validate_mimebody((None, '8bit', 'utf-8'), \ 
    422432                                ticket, True) 
    423433 
    424434    def test_md5_digest(self):