def decode_header(header): if header == None: return "" parts = email.header.decode_header(" ".join([line.strip() for line in header.split("\n")])) if len(parts) == 1 and parts[0][1] == None: return parts[0][0] return " ".join([part.decode(coding or "ascii") for part, coding in parts])
def parse_sender(header): if '<' in header and header.endswith('>'): (one, two) = header.split('<') addr = two[:-1] one = one.strip() if one.startswith('"') and one.endswith('"'): one = one[1:-1] name = email.header.decode_header(one)[0][0] return (addr, name) return (header, '')
def message_identifiers(header, reverse=False): if "<" not in header: return [] parts = header.split("<") identifier_junks = parts[1:] identifiers = [] for identifier_junk in identifier_junks: identifier = identifier_junk.split(">").pop(0) identifiers.append("<" + identifier + ">") if reverse is True: identifiers = list(reversed(identifiers)) return identifiers
def get_parsed_header(parsed_message, header_name): header = parsed_message[header_name] header = email.header.decode_header(header) if header[0][1] != None: # no charset specified header = unicode(header[0][0], header[0][1]) else: # charset specified header = unicode(header[0][0]) if header_name == "From": # Remove email address and just give name header = header.split(" <", 1)[0] if len(header) > MAX_HEADER_LENGTH: header = header[:MAX_HEADER_LENGTH-3] + "..." return header
def _decode_message(self, msg): assert isinstance(msg, email.message.Message) headers = {name: self._decode_header(value) for name, value in msg.items()} subject = self._decode_header(msg.get(u'subject', u'')) from_header = self._decode_header(msg.get(u'from', u'')) from_name, from_mail = parseaddr(from_header) text = u'' html = u'' attachments = [] for part in msg.walk(): if part.is_multipart(): continue content_type = part.get_content_type() charset = part.get_content_charset() content = part.get_payload(decode=True) disposition = self._decode_header(part.get(u'Content-Disposition', u'')) is_attachment = disposition.startswith(u'attachment') if not text and content_type == u'text/plain' and not is_attachment: text = self._decode_content(content, charset) elif not html and content_type == u'text/html' and not is_attachment: html = self._decode_content(content, charset) else: default = u'attachment{}'.format(guess_extension(content_type, u'.bin')) filename = part.get_filename(default) attachments.append(Attachment( file=ContentFile(content), name=filename, content_type=content_type, )) recipients = [] for header_name, type in ((u'to', Recipient.TYPES.TO), (u'cc', Recipient.TYPES.CC), (u'bcc', Recipient.TYPES.BCC)): header = self._decode_header(msg.get(header_name, u'')) for rcp_address in header.split(','): rcp_name, rcp_mail = parseaddr(rcp_address) if rcp_mail: recipients.append(Recipient( name=rcp_name, mail=rcp_mail, type=type, status=Recipient.STATUSES.INBOUND, )) message = Message( type=Message.TYPES.INBOUND, processed=None, from_name=from_name, from_mail=from_mail, subject=subject, text=text, html=html, headers=headers, ) message.save() for recipient in recipients: recipient.message = message recipient.save() for attachment in attachments: attachment.generic_object = message attachment.save() return message
def _decode_message(self, msg): assert isinstance(msg, email.message.Message) headers = { name: self._decode_header(value) for name, value in msg.items() } subject = self._decode_header(msg.get(u'subject', u'')) from_header = self._decode_header(msg.get(u'from', u'')) from_name, from_mail = parseaddr(from_header) text = u'' html = u'' attachments = [] for part in msg.walk(): if part.is_multipart(): continue content_type = part.get_content_type() charset = part.get_content_charset() content = part.get_payload(decode=True) disposition = self._decode_header( part.get(u'Content-Disposition', u'')) is_attachment = disposition.startswith(u'attachment') if not text and content_type == u'text/plain' and not is_attachment: text = self._decode_content(content, charset) elif not html and content_type == u'text/html' and not is_attachment: html = self._decode_content(content, charset) else: default = u'attachment%s' % guess_extension( content_type, u'.bin') filename = part.get_filename(default) attachments.append( Attachment( file=ContentFile(content), name=filename, content_type=content_type, )) recipients = [] for header_name, type in ((u'to', Recipient.TYPES.TO), (u'cc', Recipient.TYPES.CC), (u'bcc', Recipient.TYPES.BCC)): header = self._decode_header(msg.get(header_name, u'')) for rcp_address in header.split(','): rcp_name, rcp_mail = parseaddr(rcp_address) if rcp_mail: recipients.append( Recipient( name=rcp_name, mail=rcp_mail, type=type, status=Recipient.STATUSES.INBOUND, )) message = Message( type=Message.TYPES.INBOUND, processed=None, from_name=from_name, from_mail=from_mail, subject=subject, text=text, html=html, headers=headers, ) message.save() for recipient in recipients: recipient.message = message recipient.save() for attachment in attachments: attachment.generic_object = message attachment.save() return message