def _build_body_strings(self): if not self.has_built_body_strings: self.body_plain = u'' self.body_html = u'' for part in typed_subpart_iterator(self.raw, 'text', 'plain'): section_encoding = message_part_charset(part, self.raw) or self.charset section_text = utf8_encode_message_part(part, self.raw, section_encoding) if is_encoding_error(section_text): self.encoding_error = section_text else: self.body_plain += section_text for part in typed_subpart_iterator(self.raw, 'text', 'html'): section_encoding = message_part_charset(part, self.raw) or self.charset section_text = utf8_encode_message_part(part, self.raw, section_encoding) if is_encoding_error(section_text): self.encoding_error = section_text else: self.body_html += section_text self.has_built_body_strings = True
def replace(self, find, replace, trash_folder, callback=None): """Performs a body-wide string search and replace Note that this search-and-replace is pretty dumb, and will fail in, for example, HTML messages where HTML tags would alter the search string. Args: find -- the search term to look for as a string, or a tuple of items to replace with corresponding items in the replace tuple replace -- the string to replace instances of the "find" term with, or a tuple of terms to replace the corresponding strings in the find tuple trash_folder -- the name of the folder / label that is, in the current account, the trash container Returns: True on success, and in all other instances an error object """ def _set_content_transfer_encoding(part, encoding): try: del part['Content-Transfer-Encoding'] except: "" part.add_header('Content-Transfer-Encoding', encoding) valid_content_types = ('plain', 'html') for valid_type in valid_content_types: for part in typed_subpart_iterator(self.raw, 'text', valid_type): section_encoding = part['Content-Transfer-Encoding'] # If the message section doesn't advertise an encoding, # then default to quoted printable. Otherwise the module # will default to base64, which can cause problems if not section_encoding: section_encoding = "quoted-printable" else: section_encoding = section_encoding.lower() section_charset = message_part_charset(part, self.raw) new_payload_section = utf8_encode_message_part( part, self.raw, section_charset) if is_encoding_error(new_payload_section): self.encoding_error = new_payload_section return _cmd(callback, self.encoding_error) if isinstance(find, tuple) or isinstance(find, list): for i in range(0, len(find)): new_payload_section = new_payload_section.replace( find[i], replace[i]) else: new_payload_section = new_payload_section.replace( find, replace) new_payload_section = new_payload_section.encode( part._orig_charset, errors="replace") if section_encoding == "quoted-printable": new_payload_section = encodestring(new_payload_section, quotetabs=0) part.set_payload(new_payload_section, part._orig_charset) _set_content_transfer_encoding(part, "quoted-printable") elif section_encoding == "base64": part.set_payload(new_payload_section, part._orig_charset) ENC.encode_base64(part) _set_content_transfer_encoding(part, "base64") elif section_encoding in ('7bit', '8bit'): part.set_payload(new_payload_section, part._orig_charset) ENC.encode_7or8bit(part) _set_content_transfer_encoding(part, section_encoding) elif section_encoding == "binary": part.set_payload(new_payload_section, part._orig_charset) part['Content-Transfer-Encoding'] = 'binary' _set_content_transfer_encoding(part, 'binary') del part._normalized del part._orig_charset def _on_save(was_success): return _cmd(callback, was_success) return _cmd_cb(self.save, _on_save, bool(callback), trash_folder)