Last active
April 11, 2017 07:29
-
-
Save bobquest33/a7eb16601b4c6467e203c59050898d12 to your computer and use it in GitHub Desktop.
Script 1 - Finding un-read mails using python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
service="yahoo" | |
user="<email id>" | |
password="<password>" | |
server="imap.mail.yahoo.com" | |
threads="5" | |
folder="INBOX" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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