Skip to content

Instantly share code, notes, and snippets.

@alaminopu
Last active December 4, 2015 17:27
Show Gist options
  • Save alaminopu/c2e5e7bf9cfaec7fcbe2 to your computer and use it in GitHub Desktop.
Save alaminopu/c2e5e7bf9cfaec7fcbe2 to your computer and use it in GitHub Desktop.

Revisions

  1. alaminopu revised this gist Dec 4, 2015. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions export.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    #!/usr/bin/env python
    import imaplib, email
    import xlsxwriter
    from BeautifulSoup import BeautifulSoup


    # email login section
    username = 'your email username'
    @@ -57,13 +59,13 @@

    for part in msg.walk():
    if part.get_content_maintype() == 'multipart':
    source = unicode(part.as_string(),errors='ignore')
    worksheet.write(row, col+3 ,source)
    continue
    cleantext = BeautifulSoup(part.as_string()).text
    worksheet.write(row, col+3 , cleantext)
    continue
    if part.get('Content-Disposition') is None:
    #print part.as_string()
    source = unicode(part.as_string(), errors='ignore')
    worksheet.write(row, col+3 , source)
    cleantext = BeautifulSoup(part.as_string()).text
    worksheet.write(row, col+3 , cleantext)
    continue
    row = row +1

  2. alaminopu revised this gist Dec 4, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion export.py
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,7 @@
    worksheet.write(0,3, 'Message')
    sl = 0
    row = 1
    col = 1
    col = 0

    # email scrapping section
    for num in data[0].split():
  3. alaminopu created this gist Dec 4, 2015.
    74 changes: 74 additions & 0 deletions export.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    #!/usr/bin/env python
    import imaplib, email
    import xlsxwriter

    # email login section
    username = 'your email username'
    password = 'your password'
    url = "imap.mail.yahoo.com"
    email_account = imaplib.IMAP4_SSL(url)
    typ, accountDetails = email_account.login(username, password)
    if typ != 'OK':
    print 'Not able to sign in!'
    else:
    print 'Logged in'
    # Selecting
    email_account.select()
    typ, data = email_account.search(None, 'ALL')
    if typ != 'OK':
    print 'Error Searching inbox'
    else:
    print 'Searching Successful'


    # Excel sheet opening
    workbook = xlsxwriter.Workbook('emails.xlsx')
    worksheet = workbook.add_worksheet()



    # writing headers of excel sheet
    worksheet.write(0,0, 'SL')
    worksheet.write(0,1, 'From')
    worksheet.write(0,2, 'Subject')
    worksheet.write(0,3, 'Message')
    sl = 0
    row = 1
    col = 1

    # email scrapping section
    for num in data[0].split():
    # typ, data = email_account.fetch(num, '(BODY[HEADER.FIELDS (FROM TO SUBJECT)])')
    # recip,send,subj = data[0][1].split("\r\n")[:3]
    # print recip
    # print send
    # print subj

    typ, data = email_account.fetch(num, '(RFC822)')
    if typ != 'OK':
    print 'Error fetching mail.'

    msg = email.message_from_string(data[0][1])
    # wring to worksheet
    sl = sl + 1
    worksheet.write(row , col, str(sl))
    worksheet.write(row, col+1, msg['From'])
    worksheet.write(row, col+2, msg['Subject'])

    for part in msg.walk():
    if part.get_content_maintype() == 'multipart':
    source = unicode(part.as_string(),errors='ignore')
    worksheet.write(row, col+3 ,source)
    continue
    if part.get('Content-Disposition') is None:
    #print part.as_string()
    source = unicode(part.as_string(), errors='ignore')
    worksheet.write(row, col+3 , source)
    continue
    row = row +1

    # closing part
    workbook.close()
    email_account.close()
    email_account.logout()
    print "Done!"