Skip to content

Instantly share code, notes, and snippets.

@bobquest33
Last active April 11, 2017 07:29
Show Gist options
  • Save bobquest33/a7eb16601b4c6467e203c59050898d12 to your computer and use it in GitHub Desktop.
Save bobquest33/a7eb16601b4c6467e203c59050898d12 to your computer and use it in GitHub Desktop.
Script 1 - Finding un-read mails using python
service="yahoo"
user="<email id>"
password="<password>"
server="imap.mail.yahoo.com"
threads="5"
folder="INBOX"
import sys
import imaplib
import getpass
import email
import datetime
import toml
import os
import glob
import csv
import traceback
import io
M = None
for conffilename in glob.glob('conf\\*.toml'):
with open(conffilename) as conffile:
config = toml.loads(conffile.read())
service = config["service"]
user = config["user"]
passwd = config["password"]
server = config["server"]
num_worker_threads = config["threads"]
folder = config["folder"]
print(user+":"+passwd+"@"+server)
print("service:",service)
# Set the following two lines to your creds and server
M = imaplib.IMAP4_SSL(str(server))
try:
M.login(user,passwd)
except imaplib.IMAP4.error:
print("LOGIN FAILED!!! ")
rv, mailboxes = M.list()
if rv == 'OK':
print("Mailboxes:")
print(mailboxes)
rv, data = M.select(folder)
if rv == 'OK':
(retcode, messages) = M.uid('search', None, '(UNSEEN)')
if retcode == 'OK':
n = 0
print(service," has ",len(messages[0].split())," unseen mails.")
filename = service+datetime.datetime.now().strftime("%Y%m%d-%H%M%S")+".csv"
with io.open(filename,"w",encoding="utf-8",errors='ignore',newline='') as ofp:
fieldnames = ["service","uuid","from","subject","status"]
writer = csv.DictWriter(ofp, fieldnames=fieldnames)
writer.writeheader()
print('Processing ',service)
for num in messages[0].split() :
n=n+1
typ, data = M.uid('fetch', num, '(RFC822)')
for response_part in data:
if isinstance(response_part, tuple):
try:
original = email.message_from_string(response_part[1].decode('utf-8', 'ignore'))
writer.writerow({"service":service,"uuid":num,"from":original['From'],"subject":original['Subject'],"status":"unread"})
except:
traceback.print_exc()
#print("From:",original['From'])
#print("Subject:",original['Subject'])
#typ, data = mail.store(num,'+FLAGS','\\Seen')
#print(service," has ",n," unseen mails.")
print("Find details in ",filename)
M.logout()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment