diff --git a/AutoUnsubscriber.py b/AutoUnsubscriber.py index 8ddc46b..6db40c8 100644 --- a/AutoUnsubscriber.py +++ b/AutoUnsubscriber.py @@ -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 @@ -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: @@ -54,6 +53,13 @@ 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+': ') @@ -61,7 +67,7 @@ def getInfo(self): 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') @@ -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: @@ -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: @@ -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'''