0

here's what my message details looks like:

enter image description here

I used the following python code to get attached file name from this mail:

import email
mail = email.message_from_string(bytes.decode(email_body))
if mail.get_content_maintype() != 'multipart':
    continue
for part in mail.walk():
    if part.get_content_maintype() == 'multipart':
        continue
    if part.get('Content-Disposition') is None:
        continue
    fileName1 = part.get_filename()
    print(fileName1)

It prints None instead of the attachment name. Please Help!

Taazar
  • 1,545
  • 18
  • 27
Kuldeep
  • 23
  • 2
  • 8

1 Answers1

0

If the only thing you want to do is extract the filename, you can use

for part in mail.walk():
     if part.get_content_disposition() == 'attachment':
          fileName1 = part.get_filename()

This way you are not messing with other parts of the message, only with the attachment(s).

  • Also depending on how extensively you want to search for attachments, it is also possible to attach files (from my experience, images mostly) as inline. In that case you might also want to look for `part`s having `Content-Disposition` as `inline`. But be careful, as also `text` or `html` can have disposition `inline` – small_cat_destroyer Feb 03 '20 at 10:07