title: Reading Spam Folder Messages with Gmail IMap
body: <p>I have this code to use gmail imap to check unread messages, and it works good to read messages in the inbox, but I also want to read messages in the spam folder. I am not sure how I would go about this. I also only want to continue to only read Unseen messages like the script does now.  Any help or any possible solution is appreciated. This is what I have so far (this will only check inbox, not spam, I need to check unread in both): </p>
  
  <pre><code>import imaplib
  import os
  
  def check_emails(email):
      output = []
      try:
          M = imaplib.IMAP4_SSL('imap.gmail.com', 993)
          M.login(email.split(":")[0], email.split(":")[1])
          M.select()
          M.select("Inbox")
          status, data = M.search(None, "UNSEEN")
          for num in data[0].split():
              status, data = M.fetch(num, '(BODY[HEADER.FIELDS (SUBJECT)])')
              email_msg = data[0][1]
              output.append(email_msg)
  
          return output
      except Exception as e:
          return e.args
  
  
  
  emails = [line.rstrip('\n') for line in open(os.path.abspath("emails.txt"), 'r')]
  
  for email in emails:
      print("Checking email " + email)
      print(check_emails(email))
  </code></pre>