0

I have been able to figure out how to get the name of the attached file in an email. i am just stuck after that. I don't know what to do after that, I have tried using os.path.join which just gives the path i want to download the folder to and joins it with the filename. Please suggest something. Thanks.

m = imaplib.IMAP4_SSL('outlook.office365.com',993)
m.login("UN", "PW")
m.select("Inbox")

typ, msgs = mail.search(None, '(SUBJECT "qwerty")')
msgs = msgs[0].split()

for emailid in msgs:
    resp, data = mail.fetch(emailid, "(RFC822)")


    email_body = data[0][1]
    m = email.message_from_bytes(email_body)

    if m.get_content_maintype() != 'multipart':
        continue

    for part in m.walk():
        if part.get_content_maintype() == 'multipart':
            continue
        if part.get('Content-Disposition') is None:
            continue

        filename = part.get_filename()
        print(filename)
qwerty
  • 887
  • 11
  • 33
  • Take a look on this question: [get-the-gmail-attachment-filename-without-downloading-it](http://stackoverflow.com/questions/13663672/get-the-gmail-attachment-filename-without-downloading-it) – Miguel Febres May 10 '17 at 21:33
  • 1
    @MiguelFebres okay so it gets the file to the folder where the python file is. Can you tell me how can you get the file to a specific folder. If you can post your answer as an answer i can accept it as the right answer. You're the man!! :D thanks a lot!! – qwerty May 10 '17 at 23:03

1 Answers1

0

Following the sample from this link you can set the path when using the open function. (raw string by prefixing the string with r)

fp = open(r'c:\tmp\folder\' + filename, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
print '%s saved!' % filename
Community
  • 1
  • 1
Miguel Febres
  • 2,153
  • 2
  • 21
  • 31