How to get attachment’s headers from eml file?

  Kiến thức lập trình

Suppose I have a eml file with the following structure (got with print(_structure(parsed_email))):

multipart/mixed
    multipart/alternative
       text/plain
       text/html
    message/rfc822
        multipart/alternative
            text/plain
            text/html

It basically describes a eml file with another eml file as attachment (that is the message/rfc822 part). Right now I can access the “main” email’s headers with a simple snippet like

# Parse the email message
with open(file_path, "r") as file:
    parsed_email = email.message_from_file(file)
if parsed_email.is_multipart():
    headers = ""
    for k, v in parsed_email.items():
        headers += "{}: {}n".format(k, v)

What I want after is getting the attachment’s headers, and I tried with this code:

# Parse the email message
with open(file_path, "r") as file:
    parsed_email = email.message_from_file(file)
if parsed_email.is_multipart():
    print(parsed_email.get_payload())
    attachment = None
    for eml in parsed_email.get_payload():
        print(_structure(eml))
        try:
            eml_attach = eml.get('Content-Disposition').startswith('attachment')
            print(eml_attach)
            attachment = eml.get_payload(decode=True)
            print(eml.get_content_type())
            break
        except AttributeError:
            continue
    if attachment:
        print(_structure(attachment))

But when I run the code in debug mode I see my attachment variable is None. Another thing I noted is, parsed_email is a <email.message.Message object> and has the _headers variable that contains all the headers i expect.
When I’m inside the cycle and I’m using eml (that is still a <email.message.Message object>) I see that it does have the _headers variable too, but it’s different, with only a couple of headers.

So, is there a way to get the attachment’s email headers?

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT