def _find_attachments_in_email(mesg, expand_attachment, atts):
# MHTML detection
if mesg.get_content_maintype() == "multipart" and mesg.get_content_subtype() == "related":
for part in mesg.walk():
if part.is_multipart():
continue
payload = part.get_payload(decode=True)
if isinstance(payload, str) and payload.startswith('ActiveMime'):
return
for part in mesg.walk():
content_type = part.get_content_type()
if part.is_multipart():
continue
payload = part.get_payload(decode=True)
if content_type.startswith('text/') and expand_attachment:
normalized = payload.lstrip(" \t\r\n")
if any(normalized.startswith(m) for m in EMAIL_MAGIC):
new_mesg = email.message_from_string(normalized)
_find_attachments_in_email(new_mesg, expand_attachment, atts)
continue
if content_type in SAFE_MEDIA_TYPE:
continue
filename = part.get_filename()
if filename is None:
ext = mimetypes.guess_extension(content_type) or ''
filename = '<unknown>' + ext
else:
# Sanitize the header value
filename = _decode_header(filename)
filename = utils.get_filename_from_path(filename)
tempfile_path = utils.store_temp_file(payload, filename)
atts.append((tempfile_path, filename, content_type))
评论列表
文章目录