title: AttributeError: "NoneType" object has no attribute 'decode'
body: <p>I am trying to read an email from my gmail inbox in Python3. So I followed this tutorial : <a href="https://www.thepythoncode.com/article/reading-emails-in-python" rel="nofollow noreferrer">https://www.thepythoncode.com/article/reading-emails-in-python</a></p>
  <p>My code is the following :</p>
  <pre><code> username = &quot;*****@gmail.com&quot;
  password = &quot;******&quot;
  # create an IMAP4 class with SSL 
  imap = imaplib.IMAP4_SSL(&quot;imap.gmail.com&quot;)
  # authenticate
  imap.login(username, password)
  status, messages = imap.select(&quot;INBOX&quot;)
  # total number of emails
  messages = int(messages[0])
      
  
  for i in range(messages, 0, -1):
      # fetch the email message by ID
      res, msg = imap.fetch(str(i), &quot;(RFC822)&quot;)
      for response in msg:
          if isinstance(response, tuple):
              # parse a bytes email into a message object
              msg = email.message_from_bytes(response[1])
              # decode the email subject
              subject = decode_header(msg[&quot;Subject&quot;])[0][0]
              if isinstance(subject, bytes):
                  # if it's a bytes, decode to str
                  subject = subject.decode()
              # email sender
              from_ = msg.get(&quot;From&quot;)
              # if the email message is multipart
              if msg.is_multipart():
                  # iterate over email parts
                  for part in msg.walk():
                      # extract content type of email
                      content_type = part.get_content_type()
                      content_disposition = str(part.get(&quot;Content-Disposition&quot;))
  
                      # get the email body
                      body = part.get_payload(decode=True).decode()
                      print(str(body))
  
      imap.close()
      imap.logout()
      print('DONE READING EMAIL')
  </code></pre>
  <p>The libraries I am using is :</p>
  <pre><code>import imaplib
  import email
  from email.header import decode_header
  </code></pre>
  <p>However, when  I execute it I get the following error message, which I don't understand because I never have an empty email in my inbox ...</p>
  <pre><code>Traceback (most recent call last):
  
    File &quot;&lt;ipython-input-19-69bcfd2188c6&gt;&quot;, line 38, in &lt;module&gt;
      body = part.get_payload(decode=True).decode()
  
  AttributeError: 'NoneType' object has no attribute 'decode'
  </code></pre>
  <p>Anyone has an idea what my problem could be ?</p>