-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_repeated_unread_emails.py
More file actions
100 lines (83 loc) · 2.74 KB
/
Copy pathdelete_repeated_unread_emails.py
File metadata and controls
100 lines (83 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'''
delete_repeated_unread_emails.py: script to get unread repeated e-mails
from a OWA folder and delete them.
Created initially to deal with the VMware Data Recovery e-mail bug:
this deprecated (but still running in some datacenters) system has to
send a notification when it's configured to do it, i.e: once per week,
after the backup work.
But, from time to time (once per month), it just start sending e-mails
until it stops by itself and the servers enteres in error status.
That's another issue, but this script it done to clean the repeated
e-mails
'''
'''
ISSUE-01: do some research about:
"imaplib.abort: socket error: [Errno 32] Broken pipe"
mail.expunge() should go just at the end, but due to the server crashes
sometimes with a error I could not fix, nor find a solution or deal with
the error in a more civilized way. So, I had to put thisinstruction
here.
When the script crashes, the output trio (num, i, subject) are something
like this:
('11500', 92, None)
('11501', 93, None)
Repeated
('11502', 94, None)
Repeated
('11503', 95, None)
Repeated
('11504', 96, None)
Repeated
('11505', 97, None)
Repeated
[..]
raise self.abort('socket error: %s' % val)
imaplib.abort: socket error: [Errno 32] Broken pipe
In this case, it has to be just relaunched.
Unconfortable, but when I have more time I will try to find out the
solution.
'''
__author__ = "Breogan Costa"
import imaplib, email, re
import getpass
import sys, socket
# Configuration elements, fill with your own data
IMAP_SERVER = 'imap.XXXXXXX.com'
OWA_DIR = 'inbox' # make sure this is your OWA folder
EMAIL_STATUS = 'UNSEEN' # 'SEEN'
# End of configuration area
username = raw_input('Username: ')
password = getpass.getpass('Password: ')
mail = imaplib.IMAP4_SSL(IMAP_SERVER)
try:
mail.login(username, password)
except:
print(sys.exc_info()[1])
sys.exit(1)
mail.select(OWA_DIR, readonly=False)
(result, mails) = mail.uid('search', None, EMAIL_STATUS)
if result == 'OK':
print( "Number of unread e-mails:", len(mails[0].split()) )
raw_input("press a key to continue")
i = 0;
prevSubject = ""
for num in mails[0].split():
try:
typ, data = mail.fetch(num, '(RFC822)')
raw_email = data[0][1]
email_message = email.message_from_string(raw_email)
i += 1
subject = email_message.get('Subject')
print(num, i, subject )
if prevSubject == subject:
print("\tRepeated")
mail.store(num, '+FLAGS', '\\Deleted')
prevSubject = subject
mail.expunge() # TODO: fix this in other way. Check ISSUE-01
except socket.error, ex:
print("Error: ", ex)
pass
# it crashed sometimes due to sever issues & misses mail.expunge()
mail.expunge()
mail.close()
mail.logout()