-
Notifications
You must be signed in to change notification settings - Fork 5
/
t.py
273 lines (228 loc) · 7.95 KB
/
t.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from jinja2 import Environment
from jinja2 import FileSystemLoader
from boto.ses.connection import SESConnection
from setting import AWSID
from setting import AWSKEY
from setting import LEADER_MAIL
import sys
ses = SESConnection(AWSID, AWSKEY)
env = Environment(loader=FileSystemLoader('./templates/'))
def gettestinfo():
''' 取得測試用資料 '''
u = {
'nickname': u'Toomore Chiang',
'mail': u'[email protected]',
'leaderno': '8',
}
return u
def send_welcome(info):
''' 發送歡迎信
:info: dict 包含 [mail, nickname, leaderno]
'''
r = ses.send_email(
source='Toomore Chiang <[email protected]>',
subject=u'COSCUP2013 歡迎你 - {nickname}'.format(**info),
to_addresses='{mail}'.format(**info),
cc_addresses='{0}'.format(LEADER_MAIL[info.get('leaderno')]),
format='html',
return_path='[email protected]',
reply_addresses=[
LEADER_MAIL[info.get('leaderno')]],
body=template.render(**info),
)
return r
def send_first(info):
''' 發送登錄信
:info: dict 包含 [mail, nickname]
'''
r = ses.send_email(
source='Toomore Chiang <[email protected]>',
subject=u'COSCUP2013 請先完成資料登錄 - {nickname}'.format(**info),
to_addresses='{mail}'.format(**info),
format='html',
return_path='[email protected]',
reply_addresses='[email protected]',
body=template.render(**info),
)
return r
def send_transportation_fare(info):
''' 發送交通費補助
:info: dict 包含 [mail, nickname, fee]
'''
info['fee'] = u'{0:,}'.format(int(info['fee']))
r = ses.send_email(
source='Toomore Chiang <[email protected]>',
subject=u'COSCUP2013 交通費補助確認信 - {nickname} ${fee}'.format(**info),
to_addresses='{mail}'.format(**info),
format='html',
return_path='[email protected]',
reply_addresses='[email protected]',
body=template.render(**info),
)
return r
def send_weekly(no, html, mail='[email protected]'):
''' 發送週報
'''
r = ses.send_email(
source='Toomore Chiang <[email protected]>',
subject=u'COSCUP2015 Weekly #{0:02}'.format(no),
to_addresses='{0}'.format(mail),
format='html',
body=html,
)
return r
def send_register(info):
''' 發送會眾報名信
:info: dict 包含 [mail, nickname, code]
'''
r = ses.send_email(
source='COSCUP2013 Attendee <[email protected]>',
subject=u'COSCUP2013 尚未完成活動報名 - {nickname}'.format(**info),
to_addresses='{mail}'.format(**info),
format='html',
return_path='[email protected]',
reply_addresses='[email protected]',
body=template.render(**info),
)
return r
def send_speakerparty(info):
''' 發送講者晚宴通知
:info: dict 包含 [mail, nickname]
'''
r = ses.send_email(
source='COSCUP2013 Secretary <[email protected]>',
subject=u'COSCUP 講者晚宴行前通知 / Speaker’s Party Notice - {nickname}'.format(**info),
to_addresses='{mail}'.format(**info),
format='html',
return_path='[email protected]',
reply_addresses='[email protected]',
body=template.render(**info),
)
return r
def send_attendee_reminder(info):
''' 發送會眾行前通知
:info: dict 包含 [mail, nickname, foreigner]
'''
r = ses.send_email(
source='COSCUP2013 Attendee <[email protected]>',
subject=u'COSCUP 會眾行前通知信 / Reminder for Attendee - {nickname}'.format(**info),
to_addresses='{mail}'.format(**info),
format='html',
return_path='[email protected]',
reply_addresses='[email protected]',
body=template.render(**info),
)
return r
def send_survey(info):
''' 發送會眾問卷
:info: dict 包含 [mail, nickname]
'''
r = ses.send_email(
source='COSCUP2013 Attendee <[email protected]>',
subject=u"COSCUP 會眾問卷 / Attendee's Survey - {nickname}".format(**info),
to_addresses='{mail}'.format(**info),
format='html',
return_path='[email protected]',
reply_addresses='[email protected]',
body=template.render(**info),
)
return r
def send_staff_reminder(info):
''' 發送工作人員行前通知信
:info: dict 包含 [mail, nickname]
'''
r = ses.send_email(
source='COSCUP2013 Secretary <[email protected]>',
subject=u'COSCUP 工作人員行前通知 - {nickname}'.format(**info),
to_addresses='{mail}'.format(**info),
format='html',
return_path='[email protected]',
reply_addresses='[email protected]',
body=template.render(**info),
)
return r
def send_paper(info):
''' 發送會眾電子報
:info: dict 包含 [mail, nickname, issues]
'''
template = env.get_template('./paper_{issues}.htm'.format(**info))
r = ses.send_email(
source='COSCUP2013 Attendee <[email protected]>',
subject=u'COSCUP2013 電子報 - {issues} {title}'.format(**info),
to_addresses='{mail}'.format(**info),
format='html',
return_path='[email protected]',
reply_addresses='[email protected]',
body=template.render(**info),
)
return r
def send_leadervipcode(info):
''' 發送組長 VIP CODE
:info: dict 包含 [mail, nickname, code]
'''
r = ses.send_email(
source='COSCUP2013 Attendee <[email protected]>',
subject=u'COSCUP2013 VIP Code - {code}'.format(**info),
to_addresses='{mail}'.format(**info),
format='html',
return_path='[email protected]',
reply_addresses='[email protected]',
body=template.render(**info),
)
return r
def send_personal_sponsor(info):
''' 發送個人貢獻 VIP CODE
:info: dict 包含 [mail, nickname, code]
'''
r = ses.send_email(
source='COSCUP2013 Attendee <[email protected]>',
subject=u'COSCUP2013 VIP Code Personal Sponsor - {code}'.format(**info),
to_addresses='{mail}'.format(**info),
format='html',
return_path='[email protected]',
reply_addresses='[email protected]',
body=template.render(**info),
)
return r
def output(u):
''' 匯出電子報檔案 htm '''
with open('/run/shm/ppaper.htm', 'w') as f:
f.write(template.render(u).encode('utf-8'))
def sendall(sendlist, send):
''' 大量傳送 '''
for i in sendlist:
try:
send(i)
print u'SEND: {}'.format(i)
except Exception as e:
print u'ERROR: {}, {}'.format(i, e)
if __name__ == '__main__':
'''
python ./t.py output|send|sendall template_files
'''
if sys.argv[1] == 'output':
print u'{0:-^30}'.format(u'匯出電子報')
template = env.get_template(sys.argv[2])
output(gettestinfo())
elif sys.argv[1] == 'send_welcome':
print u'{0:-^30}'.format(u'寄送歡迎信')
template = env.get_template('./coscup_sendwelcome.htm')
send_welcome(gettestinfo())
elif sys.argv[1] == 'send_first':
print u'{0:-^30}'.format(u'寄送登錄信')
template = env.get_template('./coscup_first.htm')
send_first(gettestinfo())
elif sys.argv[1] == 'sendall':
print u'{0:-^30}'.format(u'大量傳送電子報')
template = env.get_template(sys.argv[2])
sendall([gettestinfo(), ])
elif sys.argv[1] == 'send_weekly':
print u'{0:-^30}'.format(u'傳送週報')
with open(sys.argv[3], 'r') as f:
send_weekly(int(sys.argv[2]), f.read(), mail=sys.argv[4])
else:
print 'output, send, sendall'
print sys.argv