-
Notifications
You must be signed in to change notification settings - Fork 1
/
toolkit_outlook.py
149 lines (125 loc) · 5 KB
/
toolkit_outlook.py
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import win32com.client as win32
import sys
import logging
default_recipients = '[email protected]'
class Outlook():
def __init__(self):
app = 'Outlook'
self.app = win32.gencache.EnsureDispatch("%s.Application" % app)
self.outlookAPI = self.app.GetNamespace("MAPI")
self.inbox = self.outlookAPI.GetDefaultFolder(
win32.constants.olFolderInbox)
self.deletedBox = self.outlookAPI.GetDefaultFolder(
win32.constants.olFolderDeletedItems)
def folderPathConvert(self, folderPath):
''' Input folder path seperated by '/'
Return folder instance'''
toFolderPathStr = 'self.inbox' + \
''.join(['''.Folders['{}']'''.format(i)
for i in folderPath.split('/') if i])
return eval(toFolderPathStr)
def send_mail(self, subj, body, recipients=None, attachment_path=None, mail_type='PlainText'):
'''
Attachment can be list or string
mail_type: PlainText/html
'''
if recipients is None:
recipients = ['[email protected]']
print("Subject: " + subj)
print("Body: " + body)
print("Send to: " + str(recipients))
logging.info("Subject: " + subj)
logging.info("Body: " + body)
logging.info("Send to: " + str(recipients))
mail = self.app.CreateItem(win32.constants.olMailItem)
for i in recipients:
mail.Recipients.Add(i)
if attachment_path is not None:
if isinstance(attachment_path, str):
mail.Attachments.Add(Source=attachment_path)
elif isinstance(attachment_path, list):
for i in attachment_path:
logging.info('Add {} as attachment'.format(i))
mail.Attachments.Add(Source=i)
mail.Subject = subj
if mail_type == 'html':
mail.HTMLBody = body
else:
mail.HTMLBody = body
try:
mail.Send()
except Exception as e:
logging.error('Failed to send email!')
else:
print('Mail sent')
logging.info('Mail sent')
def fetch_inbox_folders(self):
'''
Return the list of outlook folders
'''
fldr_iterator = self.inbox.Folders
return [i.Name for i in fldr_iterator]
def fetch_folder(self, folderName='INBOX'):
'''
Fetch the subject of mail in a folder
'''
if folderName.upper() == 'INBOX':
# Fetch inbox
folder = self.outlookAPI.GetDefaultFolder(6)
else:
folder = self.folderPathConvert(folderName)
# folder = self.inbox.Folders[folderName]
messages = folder.Items
return [message.Subject for message in messages]
def fetch_mail(self, folderName='INBOX', subject='keywords'):
'''
Fetch the HTMLBody of mail in a folder
'''
if folderName.upper() == 'INBOX':
# Fetch inbox
folder = self.outlookAPI.GetDefaultFolder(6)
else:
folder = self.folderPathConvert(folderName)
# folder = self.inbox.Folders[folderName]
messages = folder.Items
return [message.HTMLBody for message in messages if subject in message.Subject]
def empty_folder(self, folderName):
'''
Put all mails in the folder into deleted box
'''
folder = self.folderPathConvert(folderName)
messages = folder.Items
deletedItem = ['init']
while len(deletedItem):
deletedItem = []
for message in messages:
logging.info('Delete {}'.format(message.Subject))
deletedItem.append(message.Subject)
try:
message.Delete()
except Exception as e:
logging.error("Err, try again")
self.empty_folder(folderName)
def empty_junkbox(self):
'''
Purge all mails in the deleted box
'''
deletedItem = ['init']
while len(deletedItem):
deletedItem = []
for i in self.deletedBox.Items:
deletedItem.append(i.Subject)
i.Delete()
logging.info('Deleted {} mails'.format(len(deletedItem)))
logging.info("Purge done")
outlook = Outlook()
def outlook_send_mail(subj, body, recipients=None, attachment_path=None):
outlook.send_mail(subj, body, recipients=recipients,
attachment_path=attachment_path)
if __name__ == '__main__':
# folder = outlook.empty_folder('[ Notification ] Filebridge')
clear_list = ['[ Notification ] Carters', '[ Notification ] iBase',
'[ Notification ] Bose', '[ Notification ] System', '[ Notification ] Filebridge', ]
# folder = outlook.empty_folder('[ Notification ] Carters')
# outlook_send_mail('23', '33')
#