title: IMAP how to save **all** attachments
body: <p>I am trying to save excel file attachments from my inbox to a directory. My code is executing just fine because I am seeing the print outs but the attachments wont save in the file directory. Is there something I am missing in my code that is preventing the action of saving? </p>
  
  <pre><code> import email, getpass, imaplib, os, sys
  
  
  detach_dir = r'\directory link'
  
  user = "test"
  pwd = "test"
  sender_email = "example@example.com"
  
  m = imaplib.IMAP4_SSL("outlook.office365.com")
  m.login(user,pwd)
  
  
  m.select('"INBOX/somestuff"')
  
  print("ok")
  
  
  
  resp, items = m.search(None, 'FROM', '"%s"' % sender_email)
  items = items[0].split()
  
  print("ok")
  
  for emailid in items:
      resp, data = m.fetch(emailid, "(RFC822)")
      email_body = data[0][1].decode('utf-8')
      mail = email.message_from_string(email_body)
  
      print("ok")
  
      if mail.get_content_maintype() != 'multipart':
          continue
  
  
  
      subject = ""
  
      if mail["subject"] is not None:
          subject = mail["subject"]
  
      print ("["+mail["From"]+"] :" + subject)
  
      for part in mail.walk():
          if part.get_content_maintype() == 'multipart':
              continue
  
          if part.get('Content-Disposition') is None:
              continue
  
          filename = part.get_filename()
          counter = 1
  
          if not filename:
              filename = 'part-%03d%s' % (counter, 'bin')
              counter += 1
  
          att_path = os.path.join(detach_dir, filename)
  
          if not os.path.isfile(att_path) :
              fp = open(att_path, 'wb')
              fp.write(part.get_payload(decode=True))
              fp.close()
  </code></pre>
  
  <p>This code saves just one of the attachments in the subfolder but I am looking to get all attachments save to the directory:</p>
  
  <pre><code>detach_dir = r'directory link'
  m = imaplib.IMAP4_SSL("outlook.office365.com")
  m.login('user','pass')
  m.select('"INBOX/subfolder"')
  
  resp, items = m.search(None, 'All')
  items = items[0].split()
  
  for emailid in items:
      resp, data = m.fetch(emailid, "(RFC822)") 
      filename = part.get_filename()
      print(filename)
      att_path = os.path.join(detach_dir, filename)
      fp = open(att_path, 'wb')
      fp.write(part.get_payload(decode=True))
      fp.close()
  
      print('check folder')
  </code></pre>