Mail Collector

From XPUB & Lens-Based wiki

A little script to collect all Mails from a gmail label

<source lang="python">

  1. !/usr/bin/env python
  2. -*- coding:utf-8 -*-

from password import password

import imaplib import email import codecs

mail = imaplib.IMAP4_SSL('imap.gmail.com') mail.login('name@gmail.com', password) mail.list() mail.select("Label") # connect to folder with matching label

result, data = mail.uid('Search', None, "ALL") # search and return uids instead i = len(data[0].split()) myfile = open("allemails.txt", 'w') for x in range(i): latest_email_uid = data[0].split()[x] result, email_data = mail.uid('fetch', latest_email_uid, '(RFC822)') raw_email = email_data[0][1] email_message = str(email.message_from_string(raw_email)).decode("quoted-printable") myfile.write(email_message) print x print "of" print i myfile.close()