From 0406497b7987b39597a4ac8df93a795558573790 Mon Sep 17 00:00:00 2001 From: oliverclozov Date: Wed, 30 Oct 2019 20:42:23 -0400 Subject: [PATCH 1/8] Fixed a bug in the getEmails function by Changing ['BODY unsubscribe'] in self.imap.search to ['TEXT', 'unsubscribe']. I found the fix in the docs at https://imapclient.readthedocs.io/en/2.1.0/api.html?highlight=search#imapclient.IMAPClient.search. --- AutoUnsubscriber.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/AutoUnsubscriber.py b/AutoUnsubscriber.py index 8ddc46b..b1148aa 100644 --- a/AutoUnsubscriber.py +++ b/AutoUnsubscriber.py @@ -85,7 +85,7 @@ 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: @@ -176,7 +176,7 @@ def choice(userInput): break else: print('Invalid choice, please enter \'Y\' or \'N\'.\n') - while True: + '''while True: delete = input('Delete emails from '+str(self.senderList[j][1])+' (Y/N): ') d = choice(delete) if d: @@ -187,6 +187,7 @@ def choice(userInput): break else: print('Invalid choice, please enter \'Y\' or \'N\'.\n') + ''' '''Navigate to selected unsubscribe, 10 at a time''' def openLinks(self): if self.goToLinks != True: @@ -220,7 +221,7 @@ def deleteEmails(self): 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]) + DelUIDs = self.imap.search(['TEXT','unsubscribe', fromSender]) DelCount = 0 for DelUID in DelUIDs: '''Delete emails from selected providers''' From 6e5d5856b152ab0fa56c528824be02e9066654d5 Mon Sep 17 00:00:00 2001 From: oliverclozov Date: Wed, 30 Oct 2019 20:44:45 -0400 Subject: [PATCH 2/8] Fixed a bug in the getEmails function by Changing ['BODY unsubscribe'] in self.imap.search to ['TEXT', 'unsubscribe']. I found the fix in the docs at https://imapclient.readthedocs.io/en/2.1.0/api.html?highlight=search#imapclient.IMAPClient.search. --- AutoUnsubscriber.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/AutoUnsubscriber.py b/AutoUnsubscriber.py index b1148aa..4dfb206 100644 --- a/AutoUnsubscriber.py +++ b/AutoUnsubscriber.py @@ -176,7 +176,7 @@ def choice(userInput): break else: print('Invalid choice, please enter \'Y\' or \'N\'.\n') - '''while True: + while True: delete = input('Delete emails from '+str(self.senderList[j][1])+' (Y/N): ') d = choice(delete) if d: @@ -187,7 +187,6 @@ def choice(userInput): break else: print('Invalid choice, please enter \'Y\' or \'N\'.\n') - ''' '''Navigate to selected unsubscribe, 10 at a time''' def openLinks(self): if self.goToLinks != True: From f5507c94d3580a8cd5ce089c3b860bef8ef25a5a Mon Sep 17 00:00:00 2001 From: oliverclozov Date: Wed, 30 Oct 2019 20:45:29 -0400 Subject: [PATCH 3/8] Fixed a bug in the getEmails function by Changing ['BODY unsubscribe'] in self.imap.search to ['TEXT', 'unsubscribe']. I found the fix in the docs at https://imapclient.readthedocs.io/en/2.1.0/api.html?highlight=search#imapclient.IMAPClient.search. --- AutoUnsubscriber.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AutoUnsubscriber.py b/AutoUnsubscriber.py index 4dfb206..da04b6b 100644 --- a/AutoUnsubscriber.py +++ b/AutoUnsubscriber.py @@ -176,7 +176,7 @@ def choice(userInput): break else: print('Invalid choice, please enter \'Y\' or \'N\'.\n') - while True: + while True: delete = input('Delete emails from '+str(self.senderList[j][1])+' (Y/N): ') d = choice(delete) if d: From b16541c5d0647b91218cdee2c444d0c4d848d1bd Mon Sep 17 00:00:00 2001 From: Kailii Date: Thu, 10 Mar 2022 20:39:50 -0800 Subject: [PATCH 4/8] Added Support for adding your own IMAP Server! Added GMX too! --- AutoUnsubscriber.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/AutoUnsubscriber.py b/AutoUnsubscriber.py index da04b6b..cfc3727 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+': ') From 09a3528b5dfc6fa86ff9b563895ba9a013b9c425 Mon Sep 17 00:00:00 2001 From: Kailii Date: Tue, 15 Mar 2022 17:15:16 -0700 Subject: [PATCH 5/8] Fixed delete function: deletes all messages from the sender who you unsubscribed from now, not sure if that's different functionality than desired (it might've only deleted the emails with 'unsubscribe' in the body before). But anyways now deleting newsletter emails is fixed! --- AutoUnsubscriber.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/AutoUnsubscriber.py b/AutoUnsubscriber.py index cfc3727..3be7344 100644 --- a/AutoUnsubscriber.py +++ b/AutoUnsubscriber.py @@ -57,7 +57,7 @@ def getInfo(self): myimap = input('\n[myimapserver.tld] | [enter] : ') if myimap: self.user = ('Self-defined IMAP', myimap) - print('\nYou are using a'+self.user[0]+' account!\n') + print('\nYou are using a '+self.user[0]+' account!\n') getEmail = False break print('\nNo useable email type detected, try a different account') @@ -223,10 +223,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]) + sender=str(self.senderList[i][1]) + print('Searching for emails to delete from '+sender) '''Search for unsubscribe in body from selected providers''' - DelUIDs = self.imap.search(['TEXT','unsubscribe', fromSender]) + DelUIDs = self.imap.search([u'FROM', sender]) DelCount = 0 for DelUID in DelUIDs: '''Delete emails from selected providers''' From d1d7476e960793ba63f1467d1b69042aed755313 Mon Sep 17 00:00:00 2001 From: Kailii Date: Tue, 15 Mar 2022 17:34:37 -0700 Subject: [PATCH 6/8] Updated Comments --- AutoUnsubscriber.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/AutoUnsubscriber.py b/AutoUnsubscriber.py index 3be7344..499d5b7 100644 --- a/AutoUnsubscriber.py +++ b/AutoUnsubscriber.py @@ -210,8 +210,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: From a9e9c3feeaf8e46cc91504de892c27543ce8393e Mon Sep 17 00:00:00 2001 From: Kailii Date: Tue, 15 Mar 2022 17:37:03 -0700 Subject: [PATCH 7/8] Updated other comment --- AutoUnsubscriber.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AutoUnsubscriber.py b/AutoUnsubscriber.py index 499d5b7..fd4292f 100644 --- a/AutoUnsubscriber.py +++ b/AutoUnsubscriber.py @@ -225,7 +225,7 @@ def deleteEmails(self): if self.senderList[i][4] == True: sender=str(self.senderList[i][1]) print('Searching for emails to delete from '+sender) - '''Search for unsubscribe in body from selected providers''' + '''Search for UID from selected providers''' DelUIDs = self.imap.search([u'FROM', sender]) DelCount = 0 for DelUID in DelUIDs: From 0924f8f0be664d9c60ee7e5c31b8700c8502dc84 Mon Sep 17 00:00:00 2001 From: Kailii Date: Tue, 15 Mar 2022 19:43:21 -0700 Subject: [PATCH 8/8] Fixed problem with weird emails without bodies that kept the script from running --- AutoUnsubscriber.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/AutoUnsubscriber.py b/AutoUnsubscriber.py index fd4292f..6db40c8 100644 --- a/AutoUnsubscriber.py +++ b/AutoUnsubscriber.py @@ -67,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') @@ -95,8 +95,12 @@ def getEmails(self): 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: