Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29755

Getting n most recent emails using IMAP and Python

$
0
0

I'm looking to return the n (most likely 10) most recent emails from an email accounts inbox using IMAP.

So far I've cobbled together:

import imaplibfrom email.parser import HeaderParserM = imaplib.IMAP4_SSL('my.server')user = 'username'password = 'password'M.login(user, password)M.search(None, 'ALL')for i in range (1,10):    data = M.fetch(i, '(BODY[HEADER])')    header_data = data[1][0][1]    parser = HeaderParser()    msg = parser.parsestr(header_data)    print msg['subject']

This is returning email headers fine, but it seems to be a semi-random collection of emails that it gets, not the 10 most recent.

If it helps, I'm connecting to an Exchange 2010 server. Other approaches also welcome, IMAP just seemed the most appropriate given that I only wanted to read the emails not send any.


Viewing all articles
Browse latest Browse all 29755

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>