def encode(s, binary=True, maxlinelen=76, eol=NL): r"""Encode a string with base64. Each line will be wrapped at, at most, maxlinelen characters (defaults to 76 characters). If binary is False, end-of-line characters will be converted to the canonical email end-of-line sequence \r\n. Otherwise they will be left verbatim (this is the default). Each line of encoded text will end with eol, which defaults to "\n". Set this to " " if you will be using the result of this function directly in an email. """ if not s: return s if not binary: s = fix_eols(s) encvec = [] max_unencoded = maxlinelen * 3 // 4 for i in range(0, len(s), max_unencoded): enc = b2a_base64(s[i:i + max_unencoded]) if enc.endswith(NL) and eol != NL: enc = enc[:-1] + eol encvec.append(enc) return EMPTYSTRING.join(encvec)
def header_encode(header, charset='iso-8859-1', keep_eols=False, maxlinelen=76, eol=NL): if not header: return header else: if not keep_eols: header = fix_eols(header) quoted = [] if maxlinelen is None: max_encoded = 100000 else: max_encoded = maxlinelen - len(charset) - MISC_LEN - 1 for c in header: if c == ' ': _max_append(quoted, '_', max_encoded) if not hqre.match(c): _max_append(quoted, c, max_encoded) _max_append(quoted, '=%02X' % ord(c), max_encoded) joiner = eol + ' ' return joiner.join( ['=?%s?q?%s?=' % (charset, line) for line in quoted])
def encode(s, binary = True, maxlinelen = 76, eol = NL): """Encode a string with base64. Each line will be wrapped at, at most, maxlinelen characters (defaults to 76 characters). If binary is False, end-of-line characters will be converted to the canonical email end-of-line sequence \\r\\n. Otherwise they will be left verbatim (this is the default). Each line of encoded text will end with eol, which defaults to "\\n". Set this to "\r\n" if you will be using the result of this function directly in an email. """ if not s: return s if not binary: s = fix_eols(s) encvec = [] max_unencoded = maxlinelen * 3 // 4 for i in range(0, len(s), max_unencoded): # BAW: should encode() inherit b2a_base64()'s dubious behavior in # adding a newline to the encoded string? enc = b2a_base64(s[i:i + max_unencoded]) if enc.endswith(NL) and eol <> NL: enc = enc[:-1] + eol encvec.append(enc) return EMPTYSTRING.join(encvec)
def header_encode(header, charset='iso-8859-1', keep_eols=False, maxlinelen=76, eol=NL): """Encode a single header line with Base64 encoding in a given charset. Defined in RFC 2045, this Base64 encoding is identical to normal Base64 encoding, except that each line must be intelligently wrapped (respecting the Base64 encoding), and subsequent lines must start with a space. charset names the character set to use to encode the header. It defaults to iso-8859-1. End-of-line characters (\\r, \\n, \\r\\n) will be automatically converted to the canonical email line separator \\r\\n unless the keep_eols parameter is True (the default is False). Each line of the header will be terminated in the value of eol, which defaults to "\\n". Set this to "\\r\\n" if you are using the result of this function directly in email. The resulting string will be in the form: "=?charset?b?WW/5ciBtYXp66XLrIHf8eiBhIGhhbXBzdGHuciBBIFlv+XIgbWF6euly?=\\n =?charset?b?6yB3/HogYSBoYW1wc3Rh7nIgQkMgWW/5ciBtYXp66XLrIHf8eiBhIGhh?=" with each line wrapped at, at most, maxlinelen characters (defaults to 76 characters). """ # Return empty headers unchanged if not header: return header if not keep_eols: header = fix_eols(header) # Base64 encode each line, in encoded chunks no greater than maxlinelen in # length, after the RFC chrome is added in. base64ed = [] max_encoded = maxlinelen - len(charset) - MISC_LEN max_unencoded = max_encoded * 3 // 4 for i in range(0, len(header), max_unencoded): base64ed.append(b2a_base64(header[i:i + max_unencoded])) # Now add the RFC chrome to each encoded chunk lines = [] for line in base64ed: # Ignore the last character of each line if it is a newline if line.endswith(NL): line = line[:-1] # Add the chrome lines.append('=?%s?b?%s?=' % (charset, line)) # Glue the lines together and return it. BAW: should we be able to # specify the leading whitespace in the joiner? joiner = eol + ' ' return joiner.join(lines)
def header_encode(header, charset = 'iso-8859-1', keep_eols = False, maxlinelen = 76, eol = NL): """Encode a single header line with Base64 encoding in a given charset. Defined in RFC 2045, this Base64 encoding is identical to normal Base64 encoding, except that each line must be intelligently wrapped (respecting the Base64 encoding), and subsequent lines must start with a space. charset names the character set to use to encode the header. It defaults to iso-8859-1. End-of-line characters (\\r, \\n, \\r\\n) will be automatically converted to the canonical email line separator \\r\\n unless the keep_eols parameter is True (the default is False). Each line of the header will be terminated in the value of eol, which defaults to "\\n". Set this to "\\r\\n" if you are using the result of this function directly in email. The resulting string will be in the form: "=?charset?b?WW/5ciBtYXp66XLrIHf8eiBhIGhhbXBzdGHuciBBIFlv+XIgbWF6euly?=\\n =?charset?b?6yB3/HogYSBoYW1wc3Rh7nIgQkMgWW/5ciBtYXp66XLrIHf8eiBhIGhh?=" with each line wrapped at, at most, maxlinelen characters (defaults to 76 characters). """ # Return empty headers unchanged if not header: return header if not keep_eols: header = fix_eols(header) # Base64 encode each line, in encoded chunks no greater than maxlinelen in # length, after the RFC chrome is added in. base64ed = [] max_encoded = maxlinelen - len(charset) - MISC_LEN max_unencoded = max_encoded * 3 // 4 for i in range(0, len(header), max_unencoded): base64ed.append(b2a_base64(header[i:i + max_unencoded])) # Now add the RFC chrome to each encoded chunk lines = [] for line in base64ed: # Ignore the last character of each line if it is a newline if line.endswith(NL): line = line[:-1] # Add the chrome lines.append('=?%s?b?%s?=' % (charset, line)) # Glue the lines together and return it. BAW: should we be able to # specify the leading whitespace in the joiner? joiner = eol + ' ' return joiner.join(lines)
def header_encode(header, charset='iso-8859-1', keep_eols=False, maxlinelen=76, eol=NL): r"""Encode a single header line with quoted-printable (like) encoding. Defined in RFC 2045, this `Q' encoding is similar to quoted-printable, but used specifically for email header fields to allow charsets with mostly 7 bit characters (and some 8 bit) to remain more or less readable in non-RFC 2045 aware mail clients. charset names the character set to use to encode the header. It defaults to iso-8859-1. The resulting string will be in the form: "=?charset?q?I_f=E2rt_in_your_g=E8n=E8ral_dire=E7tion?\n =?charset?q?Silly_=C8nglish_Kn=EEghts?=" with each line wrapped safely at, at most, maxlinelen characters (defaults to 76 characters). If maxlinelen is None, the entire string is encoded in one chunk with no splitting. End-of-line characters (\r, \n, \r\n) will be automatically converted to the canonical email line separator \r\n unless the keep_eols parameter is True (the default is False). Each line of the header will be terminated in the value of eol, which defaults to "\n". Set this to "\r\n" if you are using the result of this function directly in email. """ if not header: return header else: if not keep_eols: header = fix_eols(header) quoted = [] if maxlinelen is None: max_encoded = 100000 else: max_encoded = maxlinelen - len(charset) - MISC_LEN - 1 for c in header: if c == ' ': _max_append(quoted, '_', max_encoded) elif not hqre.match(c): _max_append(quoted, c, max_encoded) else: _max_append(quoted, '=%02X' % ord(c), max_encoded) joiner = eol + ' ' return joiner.join( ['=?%s?q?%s?=' % (charset, line) for line in quoted])
def encode(body, binary=False, maxlinelen=76, eol=NL): if not body: return body else: if not binary: body = fix_eols(body) encoded_body = '' lineno = -1 lines = body.splitlines(1) for line in lines: if line.endswith(CRLF): line = line[:-2] elif line[-1] in CRLF: line = line[:-1] lineno += 1 encoded_line = '' prev = None linelen = len(line) for j in range(linelen): c = line[j] prev = c if bqre.match(c): c = quote(c) elif j + 1 == linelen: if c not in ' \t': encoded_line += c prev = c continue if len(encoded_line) + len(c) >= maxlinelen: encoded_body += encoded_line + '=' + eol encoded_line = '' encoded_line += c if prev and prev in ' \t': if lineno + 1 == len(lines): prev = quote(prev) if len(encoded_line) + len(prev) > maxlinelen: encoded_body += encoded_line + '=' + eol + prev else: encoded_body += encoded_line + prev else: encoded_body += encoded_line + prev + '=' + eol encoded_line = '' if lines[lineno].endswith(CRLF) or lines[lineno][-1] in CRLF: encoded_body += encoded_line + eol else: encoded_body += encoded_line encoded_line = '' return encoded_body
def encode(s, binary=True, maxlinelen=76, eol=NL): if not s: return s if not binary: s = fix_eols(s) encvec = [] max_unencoded = maxlinelen * 3 // 4 for i in range(0, len(s), max_unencoded): enc = b2a_base64(s[i:i + max_unencoded]) if enc.endswith(NL) and eol != NL: enc = enc[:-1] + eol encvec.append(enc) return EMPTYSTRING.join(encvec)
def encode(s, binary = True, maxlinelen = 76, eol = NL): if not s: return s if not binary: s = fix_eols(s) encvec = [] max_unencoded = maxlinelen * 3 // 4 for i in range(0, len(s), max_unencoded): enc = b2a_base64(s[i:i + max_unencoded]) if enc.endswith(NL) and eol != NL: enc = enc[:-1] + eol encvec.append(enc) return EMPTYSTRING.join(encvec)
def encode(body, binary = False, maxlinelen = 76, eol = NL): if not body: return body if not binary: body = fix_eols(body) encoded_body = '' lineno = -1 lines = body.splitlines(1) for line in lines: if line.endswith(CRLF): line = line[:-2] elif line[-1] in CRLF: line = line[:-1] lineno += 1 encoded_line = '' prev = None linelen = len(line) for j in range(linelen): c = line[j] prev = c if bqre.match(c): c = quote(c) elif j + 1 == linelen: if c not in ' \t': encoded_line += c prev = c continue if len(encoded_line) + len(c) >= maxlinelen: encoded_body += encoded_line + '=' + eol encoded_line = '' encoded_line += c if prev and prev in ' \t': if lineno + 1 == len(lines): prev = quote(prev) if len(encoded_line) + len(prev) > maxlinelen: encoded_body += encoded_line + '=' + eol + prev else: encoded_body += encoded_line + prev else: encoded_body += encoded_line + prev + '=' + eol encoded_line = '' if lines[lineno].endswith(CRLF) or lines[lineno][-1] in CRLF: encoded_body += encoded_line + eol else: encoded_body += encoded_line encoded_line = '' return encoded_body
def header_encode(header, charset = 'iso-8859-1', keep_eols = False, maxlinelen = 76, eol = NL): r"""Encode a single header line with quoted-printable (like) encoding. Defined in RFC 2045, this `Q' encoding is similar to quoted-printable, but used specifically for email header fields to allow charsets with mostly 7 bit characters (and some 8 bit) to remain more or less readable in non-RFC 2045 aware mail clients. charset names the character set to use to encode the header. It defaults to iso-8859-1. The resulting string will be in the form: "=?charset?q?I_f=E2rt_in_your_g=E8n=E8ral_dire=E7tion?\n =?charset?q?Silly_=C8nglish_Kn=EEghts?=" with each line wrapped safely at, at most, maxlinelen characters (defaults to 76 characters). If maxlinelen is None, the entire string is encoded in one chunk with no splitting. End-of-line characters (\r, \n, \r\n) will be automatically converted to the canonical email line separator \r\n unless the keep_eols parameter is True (the default is False). Each line of the header will be terminated in the value of eol, which defaults to "\n". Set this to "\r\n" if you are using the result of this function directly in email. """ if not header: return header else: if not keep_eols: header = fix_eols(header) quoted = [] if maxlinelen is None: max_encoded = 100000 else: max_encoded = maxlinelen - len(charset) - MISC_LEN - 1 for c in header: if c == ' ': _max_append(quoted, '_', max_encoded) elif not hqre.match(c): _max_append(quoted, c, max_encoded) else: _max_append(quoted, '=%02X' % ord(c), max_encoded) joiner = eol + ' ' return joiner.join([ '=?%s?q?%s?=' % (charset, line) for line in quoted ])
def decode_header_proper(value): def decode_fragment(fragment, encoding): if encoding is None: dammit = UnicodeDammit(fragment) if dammit.original_encoding is not None: encoding = dammit.original_encoding else: encoding = 'utf-8' return fragment.decode(encoding, 'replace') if value is None: return None value = re.sub(ecre, r'=?\1?\2?\3?= ', value) value = fix_eols(value) value = re.sub(CRLF, EMPTYSTRING, value) # Fix partially encoded headers result = ecre.search(value) if result is not None: # Check for partial encoded headers if result.start() > 0 or result.end() < len(value): # Decode every part (in reverse to modify value in-place) for match in reversed([match for match in ecre.finditer(value)]): header_fragment = decode_fragment(value[match.start():match.end()], match.group('charset')) # Read it like any other header header_subfragments = [] decoded_subfragments = decode_header(header_fragment) for subfragment, encoding in decoded_subfragments: subfragment = decode_fragment(subfragment, encoding) header_subfragments.append(subfragment) decoded_header_fragment = ''.join(header_subfragments) value = value[:match.start()] + decoded_header_fragment + value[match.end():] # Re-encode all parts as a whole value = Header(value, charset='utf-8').encode() header_fragments = [] decoded_fragments = decode_header(value) for fragment, encoding in decoded_fragments: fragment = decode_fragment(fragment, encoding) header_fragments.append(fragment) return ''.join(header_fragments)
def header_encode(header, charset = 'iso-8859-1', keep_eols = False, maxlinelen = 76, eol = NL): if not header: return header if not keep_eols: header = fix_eols(header) base64ed = [] max_encoded = maxlinelen - len(charset) - MISC_LEN max_unencoded = max_encoded * 3 // 4 for i in range(0, len(header), max_unencoded): base64ed.append(b2a_base64(header[i:i + max_unencoded])) lines = [] for line in base64ed: if line.endswith(NL): line = line[:-1] lines.append('=?%s?b?%s?=' % (charset, line)) joiner = eol + ' ' return joiner.join(lines)
def header_encode(header, charset = 'iso-8859-1', keep_eols = False, maxlinelen = 76, eol = NL): if not header: return header if not keep_eols: header = fix_eols(header) quoted = [] if maxlinelen is None: max_encoded = 100000 else: max_encoded = maxlinelen - len(charset) - MISC_LEN - 1 for c in header: if c == ' ': _max_append(quoted, '_', max_encoded) elif not hqre.match(c): _max_append(quoted, c, max_encoded) else: _max_append(quoted, '=%02X' % ord(c), max_encoded) joiner = eol + ' ' return joiner.join([ '=?%s?q?%s?=' % (charset, line) for line in quoted ])
def header_encode(header, charset='iso-8859-1', keep_eols=False, maxlinelen=76, eol=NL): if not header: return header if not keep_eols: header = fix_eols(header) base64ed = [] max_encoded = maxlinelen - len(charset) - MISC_LEN max_unencoded = max_encoded * 3 // 4 for i in range(0, len(header), max_unencoded): base64ed.append(b2a_base64(header[i:i + max_unencoded])) lines = [] for line in base64ed: if line.endswith(NL): line = line[:-1] lines.append('=?%s?b?%s?=' % (charset, line)) joiner = eol + ' ' return joiner.join(lines)
def content_as_string(self, amount=None): """ Obtain the string representation of the contained MIME message """ # Caching if self.__text: text = self.__text else: fp = StringIO() g = Generator(fp, mangle_from_=False) g.flatten(self) text = fp.getvalue() header, payload = self._splitre.split(text, 1) if len(header) < 1: text = "\r\n" + payload else: text = utils.fix_eols(header) + "\r\n\r\n" + payload self.__text = text if amount: return text[self.start_index:self.start_index + amount] else: return text[self.start_index:]
def encode(body, binary = False, maxlinelen = 76, eol = NL): r"""Encode with quoted-printable, wrapping at maxlinelen characters. If binary is False (the default), end-of-line characters will be converted to the canonical email end-of-line sequence \r\n. Otherwise they will be left verbatim. Each line of encoded text will end with eol, which defaults to "\n". Set this to "\r\n" if you will be using the result of this function directly in an email. Each line will be wrapped at, at most, maxlinelen characters (defaults to 76 characters). Long lines will have the `soft linefeed' quoted-printable character "=" appended to them, so the decoded text will be identical to the original text. """ if not body: return body else: if not binary: body = fix_eols(body) encoded_body = '' lineno = -1 lines = body.splitlines(1) for line in lines: if line.endswith(CRLF): line = line[:-2] elif line[-1] in CRLF: line = line[:-1] lineno += 1 encoded_line = '' prev = None linelen = len(line) for j in range(linelen): c = line[j] prev = c if bqre.match(c): c = quote(c) elif j + 1 == linelen: if c not in ' \t': encoded_line += c prev = c continue if len(encoded_line) + len(c) >= maxlinelen: encoded_body += encoded_line + '=' + eol encoded_line = '' encoded_line += c if prev and prev in ' \t': if lineno + 1 == len(lines): prev = quote(prev) if len(encoded_line) + len(prev) > maxlinelen: encoded_body += encoded_line + '=' + eol + prev else: encoded_body += encoded_line + prev else: encoded_body += encoded_line + prev + '=' + eol encoded_line = '' if lines[lineno].endswith(CRLF) or lines[lineno][-1] in CRLF: encoded_body += encoded_line + eol else: encoded_body += encoded_line encoded_line = '' return encoded_body
def encode(body, binary=False, maxlinelen=76, eol=NL): """Encode with quoted-printable, wrapping at maxlinelen characters. If binary is False (the default), end-of-line characters will be converted to the canonical email end-of-line sequence \\r\\n. Otherwise they will be left verbatim. Each line of encoded text will end with eol, which defaults to "\\n". Set this to "\\r\\n" if you will be using the result of this function directly in an email. Each line will be wrapped at, at most, maxlinelen characters (defaults to 76 characters). Long lines will have the `soft linefeed' quoted-printable character "=" appended to them, so the decoded text will be identical to the original text. """ if not body: return body if not binary: body = fix_eols(body) # BAW: We're accumulating the body text by string concatenation. That # can't be very efficient, but I don't have time now to rewrite it. It # just feels like this algorithm could be more efficient. encoded_body = '' lineno = -1 # Preserve line endings here so we can check later to see an eol needs to # be added to the output later. lines = body.splitlines(1) for line in lines: # But strip off line-endings for processing this line. if line.endswith(CRLF): line = line[:-2] elif line[-1] in CRLF: line = line[:-1] lineno += 1 encoded_line = '' prev = None linelen = len(line) # Now we need to examine every character to see if it needs to be # quopri encoded. BAW: again, string concatenation is inefficient. for j in range(linelen): c = line[j] prev = c if bqre.match(c): c = quote(c) elif j+1 == linelen: # Check for whitespace at end of line; special case if c not in ' \t': encoded_line += c prev = c continue # Check to see to see if the line has reached its maximum length if len(encoded_line) + len(c) >= maxlinelen: encoded_body += encoded_line + '=' + eol encoded_line = '' encoded_line += c # Now at end of line.. if prev and prev in ' \t': # Special case for whitespace at end of file if lineno + 1 == len(lines): prev = quote(prev) if len(encoded_line) + len(prev) > maxlinelen: encoded_body += encoded_line + '=' + eol + prev else: encoded_body += encoded_line + prev # Just normal whitespace at end of line else: encoded_body += encoded_line + prev + '=' + eol encoded_line = '' # Now look at the line we just finished and it has a line ending, we # need to add eol to the end of the line. if lines[lineno].endswith(CRLF) or lines[lineno][-1] in CRLF: encoded_body += encoded_line + eol else: encoded_body += encoded_line encoded_line = '' return encoded_body
def header_encode(header, charset="iso-8859-1", keep_eols=False, maxlinelen=76, eol=NL): """Encode a single header line with quoted-printable (like) encoding. Defined in RFC 2045, this `Q' encoding is similar to quoted-printable, but used specifically for email header fields to allow charsets with mostly 7 bit characters (and some 8 bit) to remain more or less readable in non-RFC 2045 aware mail clients. charset names the character set to use to encode the header. It defaults to iso-8859-1. The resulting string will be in the form: "=?charset?q?I_f=E2rt_in_your_g=E8n=E8ral_dire=E7tion?\\n =?charset?q?Silly_=C8nglish_Kn=EEghts?=" with each line wrapped safely at, at most, maxlinelen characters (defaults to 76 characters). If maxlinelen is None, the entire string is encoded in one chunk with no splitting. End-of-line characters (\\r, \\n, \\r\\n) will be automatically converted to the canonical email line separator \\r\\n unless the keep_eols parameter is True (the default is False). Each line of the header will be terminated in the value of eol, which defaults to "\\n". Set this to "\\r\\n" if you are using the result of this function directly in email. """ # Return empty headers unchanged if not header: return header if not keep_eols: header = fix_eols(header) # Quopri encode each line, in encoded chunks no greater than maxlinelen in # length, after the RFC chrome is added in. quoted = [] if maxlinelen is None: # An obnoxiously large number that's good enough max_encoded = 100000 else: max_encoded = maxlinelen - len(charset) - MISC_LEN - 1 for c in header: # Space may be represented as _ instead of =20 for readability if c == ' ': _max_append(quoted, '_', max_encoded) # These characters can be included verbatim elif not hqre.match(c): _max_append(quoted, c, max_encoded) # Otherwise, replace with hex value like =E2 else: _max_append(quoted, "=%02X" % ord(c), max_encoded) # Now add the RFC chrome to each encoded chunk and glue the chunks # together. BAW: should we be able to specify the leading whitespace in # the joiner? joiner = eol + ' ' return joiner.join(['=?%s?q?%s?=' % (charset, line) for line in quoted])