def fetchmail(mail,first,last): """ Fetches all the mails in the inbox from a logged in imap server. Takes three arguments: imap server variable, starting date and end date. Internally calls the makefile function. """ mail.select('inbox') day,month,year = comparedate.breakdate(first) date = datetime.date(year,month,day).strftime("%d-%b-%Y") result, data = mail.uid('search', None, '(SENTSINCE {date})'.format(date=date)) if result!='OK': sys.stderr.write('Error searching mailbox') sys.exit(1) idstring = data[0] uids = idstring.split() for i in uids: res, data = mail.uid('fetch',i,'(RFC822)') print '\nReading mail with id:',i if res != 'OK': print 'Error fetching mail' continue text = data[0][1] msg = email.message_from_string(text) match_date = re.search(r'\d+\s\w+\s\d+',msg['Date']) date = match_date.group() checkdate = comparedate.compare(date,last) if checkdate == 1: break makefile(text,msg,i,date) print 'Processed mail id '+ i
def main(): args = sys.argv[1:] username = '' password = '' if len(args)==2: username = args[0] password = args[1] else: username =raw_input('Enter your email id: ') print 'Password will not echo on the screen, keep typing!!!' password = getpass.getpass('Enter password: '******'Enter date in the format : \'1 Jan 1990\'' print 'Leave empty to set it to yesterday' d = datetime.timedelta(days=1) first = raw_input('Enter start date: ') print 'Leave empty to set it to today' last = raw_input('Enter end date: ') if not first: first = (datetime.date.today() - d).strftime("%d %b %Y") if not last: last = datetime.date.today().strftime("%d %b %Y") comparedates = comparedate.compare(first,last) if comparedates == 1: first,last = last,first fetchmail(mail,first,last) mail.close() mail.logout()