title: Reading unread emails using python script
body: <p>I am trying to read all the unread emails from the gmail account.
  The above code is able to make connection but is unable to fetch the emails.</p>
  
  <p>I want to print the content of each email.</p>
  
  <p>I am getting the error as can't concat int to bytes.</p>
  
  <p>code:</p>
  
  <pre><code>import smtplib
  import time
  import imaplib
  import email
  
  def read_email_from_gmail():
          mail = imaplib.IMAP4_SSL('imap.gmail.com')
          mail.login('my_mail','my_pwd')
          mail.select('inbox')
  
          result, data = mail.search(None, 'ALL')
          mail_ids = data[0]
  
          id_list = mail_ids.split()   
          first_email_id = int(id_list[0])
          latest_email_id = int(id_list[-1])
  
  
          for i in range(latest_email_id,first_email_id, -1):
              result, data = mail.fetch(i, '(RFC822)' )
  
              for response_part in data:
                  if isinstance(response_part, tuple):
                      msg = email.message_from_string(response_part[1])
                      email_subject = msg['subject']
                      email_from = msg['from']
                      print ('From : ' + email_from + '\n')
                      print ('Subject : ' + email_subject + '\n')
  
  
  
  print(read_email_from_gmail())
  </code></pre>
  
  <p>error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:/Users/devda/Desktop/Internship/access_email.py", line 32, in &lt;module&gt;
      print(read_email_from_gmail())
    File "C:/Users/devda/Desktop/Internship/access_email.py", line 20, in read_email_from_gmail
      result, data = mail.fetch(i, '(RFC822)' )
    File "C:\Users\devda\AppData\Local\Programs\Python\Python36\lib\imaplib.py", line 529, in fetch
      typ, dat = self._simple_command(name, message_set, message_parts)
    File "C:\Users\devda\AppData\Local\Programs\Python\Python36\lib\imaplib.py", line 1191, in _simple_command
      return self._command_complete(name, self._command(name, *args))
    File "C:\Users\devda\AppData\Local\Programs\Python\Python36\lib\imaplib.py", line 956, in _command
      data = data + b' ' + arg
  TypeError: can't concat int to bytes
  &gt;&gt;&gt; 
  </code></pre>
  
  <p>I followed the tutorial from <a href="https://codehandbook.org/how-to-read-email-from-gmail-using-python/" rel="nofollow noreferrer">here</a></p>
  
  <p>What I want to do is to extract content from email which is shown in image
  <a href="https://i.stack.imgur.com/4CxC8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4CxC8.png" alt="Email"></a></p>