def encode_7bit(self, encoder=None): """.. versionadded:: 0.3.12 Forces the message into 7-bit encoding such that it can be sent to SMTP servers that do not support the ``8BITMIME`` extension. If the ``encoder`` function is not given, this function is relatively cheap and will just check the message body for 8-bit characters (raising :py:exc:`UnicodeDecodeError` if any are found). Otherwise, this method can be very expensive. It will parse the entire message into MIME parts in order to encode parts that are not 7-bit. :param encoder: Optional function from :mod:`email.encoders` used to encode MIME parts that are not 7-bit. :raises: UnicodeDecodeError """ header_data, msg_data = self.flatten() # header data may contain ascii chars, even if RFCs disallow it # excepted with SMTPUTF8 extension. Some MTA work like that. encoded_header_data = utf8only_encode(header_data) try: msg_data.decode('ascii') except UnicodeError: if not encoder: raise self._encode_parts(encoded_header_data, msg_data, encoder)
def parsestr(self, text, headersonly=False): if isinstance(text, unicode): # difference with vanilla is we encode using utf-8, not ascii ret = self.parse(StringIO(utf8only_encode(text)), headersonly=headersonly) else: ret = _Parser.parsestr(self, text, headersonly) # homogeneous return type with py3 ret._headers = [(utf8only_decode(i), utf8only_decode(j)) for i, j in ret._headers] return ret