-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemaildhmn.py
177 lines (147 loc) · 5 KB
/
emaildhmn.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"""
Copyright (c) 2015 Mike Putnam <[email protected]>
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
"""
from __future__ import print_function
import argparse
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import dateutil.parser
"""
Based on:
http://codecomments.wordpress.com/2008/01/04/python-gmail-smtp-example/
Added argparse bits to move gmail credentials out of the script.
Hardcoded the schedule and messages into the script.
"""
def sendMail(u, p, r, subject, text):
gmailUser = u
gmailPassword = p
recipient = r
msg = MIMEMultipart()
msg['From'] = gmailUser
msg['To'] = recipient
msg['Subject'] = subject
msg.attach(MIMEText(text))
mailServer = smtplib.SMTP('smtp.gmail.com', 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmailUser, gmailPassword)
mailServer.sendmail(gmailUser, recipient, msg.as_string())
mailServer.close()
footer = """
--
Want a place to track your project, or look at what others are working on?
Check out the Trello Project Board! https://trello.com/b/eSPKdh9O/dhmn-project-board
Appleton Makerspace
121R B North Douglas St
Appleton, WI 54914
""" # NOQA
projects = """
Makers! It's time to share... what have you been hacking on or making?
Reply to this email with a brief run-down of whatever projects have been keeping you busy.
*** This weekly sharing encouragement idea shamelessly stolen from reMMinderbot at Milwaukee Makerspace http://groups.google.com/group/milwaukeemakerspace
""" # NOQA
def formatEvents(events):
lines = []
for event in events:
start = dateutil.parser.parse(
event['start'].get(
'dateTime',
event['start'].get('date')
)
)
end = dateutil.parser.parse(
event['end'].get(
'dateTime',
event['end'].get('date')
)
)
lines.append(
' - {summary}! {start}-{end}'.format(
summary=event['summary'],
start=start.strftime("%A %-I:%M%p"),
end=end.strftime("%-I:%M%p"),
)
)
return '\n\n'.join(lines)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='Send weekly events to dhmn-discuss.'
)
parser.add_argument(
'--user',
help='Gmail user.'
)
parser.add_argument(
'--password',
help='Gmail password.'
)
parser.add_argument(
'--service_account_name',
help='Google Calendar API Service Account name'
)
parser.add_argument(
'--path_to_key',
help='Path to Google Calendar API private key'
)
parser.add_argument(
'--calendar_id',
help='Calendar from which to pull events. ' +
'Must be shared to the Service Account user.'
)
parser.add_argument(
'--days',
help='Days from now to search through for events.'
)
parser.add_argument(
'recipient'
)
args = parser.parse_args()
import datetime
d = datetime.date.today()
datestring = '{0:04d}-{1:02d}-{2:02d}'.format(d.year, d.month, d.day)
from eventcalendar import EventCalendar
calendar = EventCalendar(
args.service_account_name,
args.path_to_key,
args.calendar_id,
datetime.timedelta(days=int(args.days)),
)
from recentwikiedits import RecentWikiEdits
recent_wiki_edits = RecentWikiEdits()
separator = '\n---------------------------------------------------------\n'
this_week_email_body = separator
this_week_email_body += 'This Week at the Appleton Makerspace'
this_week_email_body += separator + '\n'
this_week_email_body += formatEvents(calendar.fetch_events())
this_week_email_body += '\n\n'
this_week_email_body += separator
this_week_email_body += 'Recent Wiki Edits'
this_week_email_body += separator + '\n'
this_week_email_body += recent_wiki_edits.fetch()
this_week_email_body += '\n\n'
sendMail(
args.user,
args.password,
args.recipient,
"This Week at the Appleton Makerspace",
this_week_email_body + footer,
)
sendMail(
args.user,
args.password,
args.recipient,
"What Have You Been Hacking/Making? [" + datestring + " edition]",
projects
)