-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendmail.py
More file actions
120 lines (88 loc) · 3.73 KB
/
Copy pathsendmail.py
File metadata and controls
120 lines (88 loc) · 3.73 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import sys, os, stat, string, time, datetime, re
from smtplib import SMTP
from email.MIMEText import MIMEText
from icalendar import Calendar, Event
cal = Calendar.from_string(open('<pathtoyourcalender>\\default.ics','rb').read())
# formatTodo: Function to format a to-do mail
def formatToDo(cal, format):
# Load the email templates
f = open('templates/mail_template.html', 'r')
mail_template = f.read()
f.close()
f = open('templates/task_template.html', 'r')
task_template = f.read()
f.close()
# Build the array of completed, wip and todo items from the calender
completed_items = []
wip_items = []
todo_items = []
for component in cal.walk('VTODO'):
if not 'STATUS' in component:
continue
if component['STATUS'] == 'COMPLETED':
days = abs(component['COMPLETED'].dt.replace(tzinfo=None) - datetime.datetime.now().replace(tzinfo=None)).days
if days < 7: # We're interested only in completed items from the last 7 days.
completed_items.append(component)
else:
if component['STATUS'] == 'IN-PROCESS':
wip_items.append(component)
else:
todo_items.append(component)
# Build report of completed items
sl = 0
completed_mail = ''
for component in completed_items:
sl = sl + 1
progress = ''
if 'PERCENT-COMPLETE' in component:
progress = str(component['PERCENT-COMPLETE']) + '% complete'
description = ''
if 'DESCRIPTION' in component:
description = component['DESCRIPTION']
mailstring = task_template % ( sl, component['SUMMARY'], component['STATUS'], component['PRIORITY'], progress, description)
completed_mail = completed_mail + mailstring
# Build report of wip items
wip_mail = ''
for component in wip_items:
sl = sl + 1
progress = ''
if 'PERCENT-COMPLETE' in component:
progress = str(component['PERCENT-COMPLETE']) + '% complete'
description = ''
if 'DESCRIPTION' in component:
description = component['DESCRIPTION']
mailstring = task_template % ( sl, component['SUMMARY'], component['STATUS'], component['PRIORITY'], progress, description)
wip_mail = wip_mail + mailstring
# Build report of todo items
todo_mail = ''
for component in todo_items:
sl = sl + 1
progress = ''
if 'PERCENT-COMPLETE' in component:
progress = str(component['PERCENT-COMPLETE']) + '% complete'
description = ''
if 'DESCRIPTION' in component:
description = component['DESCRIPTION']
mailstring = task_template % ( sl, component['SUMMARY'], component['STATUS'], component['PRIORITY'], progress, description)
todo_mail = todo_mail + mailstring
# Put the lists into the email template
mail = mail_template % (completed_mail, wip_mail, todo_mail)
return mail
# Function to send an email status update
def sendemail(destination):
SMTPserver = '<yourmailserver>'
sender = '<youremail@yourcompany.com>'
subject="Goals as on " + str(datetime.datetime.now().date()) + " (<Your Name>)" # Update this
content= formatToDo(cal, 'text')
try:
msg = MIMEText(content, 'html')
msg['Subject']= subject
conn = SMTP(SMTPserver)
conn.set_debuglevel(True)
try:
conn.sendmail(sender, destination, msg.as_string())
finally:
conn.close()
except Exception, exc:
sys.exit( "mail failed: %s" % str(exc) )
sendemail(["<yourmanager@yourcompany.com>", "<yourteam@yourcompany.com>"])