-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_email.py
63 lines (46 loc) · 2.42 KB
/
read_email.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
import win32com.client
import sys
import os
import datetime
def fill_table(messages, yesterday, this_file):
data = []
for message in reversed(messages):
date = message.SentOn.strftime("%d-%m-%y")
if date == yesterday:
data.append(message.Subject)
data.sort()
return data
def write_messages(table, this_file):
for i in table:
this_file.writelines(i+"\n")
print(i)
print("\n" + str(len(table)) + " job anomalies on " + str(yesterday))
this_file.write("\n"+str(len(table)) + " job anomalies on " + str(yesterday))
if __name__ == "__main__":
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
job_failing_inbox = outlook.GetDefaultFolder(6).Folders.Item('BiSandbox').Folders.Item('Job failing')
job_duration_unusual_inbox = outlook.GetDefaultFolder(6).Folders.Item('BiSandbox').Folders.Item('Job duration unusual')
job_failing_messages = job_failing_inbox.Items
job_duration_unusual_messages = job_duration_unusual_inbox.Items
yesterday = (datetime.date.today() - datetime.timedelta(days=1)).strftime("%d-%m-%y")
print(" ")
for account in outlook.Accounts:
print(account.DeliveryStore.DisplayName)
print(" ")
job_failing_file_name = "job_failing_report_"+yesterday+".txt"
job_failing_file = open(job_failing_file_name, 'w+')
job_duration_unusual_file_name = "job_duration_unusual_report_"+yesterday+".txt"
job_duration_unusual_file = open(job_duration_unusual_file_name, 'w+')
job_duration_table = fill_table(job_duration_unusual_messages,yesterday,job_duration_unusual_file)
job_failing_table = fill_table(job_failing_messages, yesterday, job_failing_file)
write_messages(job_duration_table, job_duration_unusual_file)
write_messages(job_failing_table, job_failing_file)
mail = outlook.CreateItem(1)
mail.To = '[email protected]'
mail.Subject = 'Job error report for '+ yesterday
mail.Body = "V priponki pošiljam job error report za omenjeni datum. Sporočilo je avtomatsko BEEP BOOP BOP. LP ZZ"
mail.Attachments.Add('D:/GitHub/python-for-outlook/'+job_failing_file)
mail.Attachments.Add('D:/GitHub/python-for-outlook/'+job_duration_unusual_file)
#mail.CC = '[email protected]'
mail.Send()
#send_mail(mail, yesterday, job_duration_unusual_file, job_failing_file)