title: python imaplib to get gmail inbox subjects titles and sender name
body: <p>I'm using pythons imaplib to connect to my gmail account.  I want to retrieve the top 15 messages (unread or read, it doesn't matter) and display just the subjects and sender name (or address) but don't know how to display the contents of the inbox.</p>
  
  <p>Here is my code so far (successful connection)</p>
  
  <pre><code>import imaplib
  
  mail = imaplib.IMAP4_SSL('imap.gmail.com')
  mail.login('mygmail@gmail.com', 'somecrazypassword')
  mail.list()
  mail.select('inbox')
  
  #need to add some stuff in here
  
  mail.logout()
  </code></pre>
  
  <p>I believe this should be simple enough, I'm just not familiar enough with the commands for the imaplib library.  Any help would be must appreciated...</p>
  
  <p><strong>UPDATE</strong>
  thanks to Julian I can iterate through each message and retrieve the entire contents with: </p>
  
  <pre><code>typ, data = mail.search(None, 'ALL')
  for num in data[0].split():
     typ, data = mail.fetch(num, '(RFC822)')
     print 'Message %s\n%s\n' % (num, data[0][1])
  mail.close()
  </code></pre>
  
  <p>but I'm wanting just the subject and the sender. Is there a imaplib command for these items or will I have to parse the entire contents of data[0][1] for the text: Subject, and Sender?</p>
  
  <p><strong>UPDATE</strong>
  OK, got the subject and sender part working but the iteration (1, 15) is done by desc order apparently showing me the oldest messages first.  How can I change this?  I tried doing this:</p>
  
  <pre><code>for i in range( len(data[0])-15, len(data[0]) ):
       print data
  </code></pre>
  
  <p>but that just gives me <code>None</code> for all 15 iterations... any ideas? I've also tried <code>mail.sort('REVERSE DATE', 'UTF-8', 'ALL')</code> but gmail doesnt support the .sort() function</p>
  
  <p><strong>UPDATE</strong>
  Figured out a way to do it: </p>
  
  <pre><code>#....^other code is the same as above except need to import email module
  mail.select('inbox')
  typ, data = mail.search(None, 'ALL')
  ids = data[0]
  id_list = ids.split()
  #get the most recent email id
  latest_email_id = int( id_list[-1] )
  
  #iterate through 15 messages in decending order starting with latest_email_id
  #the '-1' dictates reverse looping order
  for i in range( latest_email_id, latest_email_id-15, -1 ):
     typ, data = mail.fetch( i, '(RFC822)' )
  
     for response_part in data:
        if isinstance(response_part, tuple):
            msg = email.message_from_string(response_part[1])
            varSubject = msg['subject']
            varFrom = msg['from']
  
     #remove the brackets around the sender email address
     varFrom = varFrom.replace('&lt;', '')
     varFrom = varFrom.replace('&gt;', '')
  
     #add ellipsis (...) if subject length is greater than 35 characters
     if len( varSubject ) &gt; 35:
        varSubject = varSubject[0:32] + '...'
  
     print '[' + varFrom.split()[-1] + '] ' + varSubject
  </code></pre>
  
  <p>this gives me the most recent 15 message subject and sender address in decending order as requested! Thanks to all who helped!</p>