Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions AutoUnsubscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
('Hotmail','imap-mail.outlook.com'),('Yahoo','imap.mail.yahoo.com'),
('ATT','imap.mail.att.net'),('Comcast','imap.comcast.net'),
('Verizon','incoming.verizon.net'),('AOL','imap.aol.com'),
('Zoho','imap.zoho.com')]
('Zoho','imap.zoho.com'),('GMX','imap.gmx.com')]

#add to words if more words found
'''Key words for unsubscribe link - add more if found'''
words = ['unsubscribe','subscription','optout']

class AutoUnsubscriber():

def __init__(self):
self.email = ''
self.user = None
Expand All @@ -40,8 +39,8 @@ def __init__(self):
'''Get initial user info - email, password, and service provider'''
def getInfo(self):
print('This program searchs your email for junk mail to unsubscribe from and delete')
print('Suported emails: Gmail, Outlook, Hotmail, Yahoo, AOL, Zoho,')
print('AT&T, Comcast, and Verizon')
print('Supported emails: Gmail, Outlook, Hotmail, Yahoo, AOL, Zoho,')
print('GMX, AT&T, Comcast, and Verizon')
print('Please note: you may need to allow access to less secure apps')
getEmail = True
while getEmail:
Expand All @@ -54,14 +53,21 @@ def getInfo(self):
getEmail = False
break
if self.user == None:
print('\nEmail type not recognized, enter an imap server, or press enter to try a different email address:\n')
myimap = input('\n[myimapserver.tld] | [enter] : ')
if myimap:
self.user = ('Self-defined IMAP', myimap)
print('\nYou are using a '+self.user[0]+' account!\n')
getEmail = False
break
print('\nNo useable email type detected, try a different account')
self.password = getpass.getpass('Enter password for '+self.email+': ')

'''Log in to IMAP server, argument determines whether readonly or not'''
def login(self, read=True):
try:
self.imap = imapclient.IMAPClient(self.user[1], ssl=True)
self.imap._MAXLINE = 10000000
#self.imap._MAXLINE = 10000000
self.imap.login(self.email, self.password)
self.imap.select_folder('INBOX', readonly=read)
print('\nLog in successful\n')
Expand All @@ -85,12 +91,16 @@ def accessServer(self, readonly=True):
'''
def getEmails(self):
print('Getting emails with unsubscribe in the body\n')
UIDs = self.imap.search(['BODY unsubscribe'])
UIDs = self.imap.search([u'TEXT','unsubscribe'])
raw = self.imap.fetch(UIDs, ['BODY[]'])
print('Getting links and addresses\n')
for UID in UIDs:
'''Get address and check if sender already in senderList'''
msg = pyzmail.PyzMessage.factory(raw[UID][b'BODY[]'])
'''If Body exists (resolves weird error with no body emails from Yahoo), then
Get address and check if sender already in senderList '''
if b'BODY[]' in raw[UID]: msg = pyzmail.PyzMessage.factory(raw[UID][b'BODY[]'])
else:
print("Odd Email at UID: "+str(UID)+"; SKIPPING....")
continue
sender = msg.get_addresses('from')
trySender = True
for spammers in self.senderList:
Expand Down Expand Up @@ -204,8 +214,8 @@ def openLinks(self):
counter = 0

'''Log back into IMAP servers, NOT in readonly mode, and delete emails from
selected providers. Note: only deleting emails with unsubscribe in the body.
Emails from provider without unsubscribe in the body will not be deleted.
selected providers. Note: Deletes all emails from unsubscribed sender.
Emails from provider without unsubscribe in the body will be deleted.
'''
def deleteEmails(self):
if self.delEmails != True:
Expand All @@ -217,10 +227,10 @@ def deleteEmails(self):
DelTotal = 0
for i in range(len(self.senderList)):
if self.senderList[i][4] == True:
print('Searching for emails to delete from '+str(self.senderList[i][1]))
fromSender = 'FROM '+str(self.senderList[i][1])
'''Search for unsubscribe in body from selected providers'''
DelUIDs = self.imap.search(['BODY unsubscribe', fromSender])
sender=str(self.senderList[i][1])
print('Searching for emails to delete from '+sender)
'''Search for UID from selected providers'''
DelUIDs = self.imap.search([u'FROM', sender])
DelCount = 0
for DelUID in DelUIDs:
'''Delete emails from selected providers'''
Expand Down