Question

I have an issue that I find perplexing. I'm using imaplib to search for emails in a Sent folder and then exporting them into a csv. It works perfectly fine, except that it's not getting all emails. It seems to be getting emails from midnight to 6PM that day and then doesn't get any emails from 6PM to 11:59PM. I believe I read somewhere that imaplib uses GMT (maybe I'm wrong?) so could this be the issue (I'm EST)? I'm not sure how to approach this other than querying the community.

Code sample below. I can provide any information necessary to aide in assistance. Thanks in advance!

import imaplib
import datetime
import csv

TODAYSDATE = datetime.date.today().strftime("%d-%b-%Y").strip()
ONEDAYAGO = (datetime.date.today() - datetime.timedelta(days=1)).strftime("%d-%b-%Y").strip()

SERVER = 'server.email.com'
IMAPPORT = 993

CSVFILE = "EMAILS.csv"
EMAILS = []
TO = []

mailbox = imaplib.IMAP4_SSL(SERVER, IMAPPORT)
mailbox.login(USER, PASS)
mailbox.select('Sent')

typ, data = mailbox.search(None, '(since %s before %s)' % (ONEDAYAGO, TODAYSDATE))

for sentto in data[0].split():
    typ, data = mailbox.fetch(sentto, '(RFC822.SIZE BODY[HEADER.FIELDS (TO)])')
    message = data[0][1].lstrip('To: ').strip() + ' '
    print message
    TO.append([message])

TO.insert(0, ['TO'])

TO_FINAL = [l[0] for l in TO]

mailbox.close()
mailbox.logout()

rows = zip(TO_FINAL)

print rows

CSV = open(CSVFILE, 'wb')
WRITECSV = csv.writer(CSV)

for row in rows:
    WRITECSV.writerow(row)

No correct solution

OTHER TIPS

I figured out my issue. I was not properly closing out my CSV file. Booo on me! This helped me identify and solve my issue --> https://stackoverflow.com/questions/15772275/writing-to-csv-from-list-write-row-seems-to-stop-in-a-strange-place

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top