def _write_headers(self, msg): # This is almost the same as the string version, except for handling # strings with 8bit bytes. for h in msg._headers: self.write('%s: ' % h.name) if isinstance(h, str): v = h.source if h.source is not None else h if _has_surrogates(v): if not self.policy.must_be_7bit: # If we have raw 8bit data in a byte string, we have no idea # what the encoding is. There is no safe way to split this # string. If it's ascii-subset, then we could do a normal # ascii split, but if it's multibyte then we could break the # string. There's no way to know so the least harm seems to # be to not split the string and risk it being too long. self.write(v + NL) continue h = Header(v, charset=_charset.UNKNOWN8BIT, header_name=h.name) else: h = Header(v, header_name=h.name) self.write( h.encode(linesep=self._NL, maxlinelen=self._maxheaderlen) + self._NL) # A blank line always separates headers from body self.write(self._NL)
def _handle_text(self, msg): # If the string has surrogates the original source was bytes, so # just write it back out. if msg._payload is None: return if _has_surrogates(msg._payload) and not self.policy.must_be_7bit: self.write(msg._payload) else: super(BytesGenerator, self)._handle_text(msg)
def _handle_text(self, msg): payload = msg.get_payload() if payload is None: return if not isinstance(payload, str): raise TypeError('string payload expected: %s' % type(payload)) if _has_surrogates(msg._payload): charset = msg.get_param('charset') if charset is not None: del msg['content-transfer-encoding'] msg.set_payload(payload, charset) payload = msg.get_payload() if self._mangle_from_: payload = fcre.sub('>From ', payload) self.write(payload)