-
Notifications
You must be signed in to change notification settings - Fork 0
/
redflag.py
271 lines (202 loc) · 6.26 KB
/
redflag.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
"""
Mailgun Mailer
--------------
Send outgoing mail using requests, jutil, and mailgun.
"""
import requests
from jutil.decorators import constant
from jutil.environment import Deployment, get_unicode
MAILGUN_API_URL = unicode("https://api.mailgun.net/v2")
MAILGUN_MESSAGES_SUFFIX = unicode("messages")
_mailgun_api_key = None
_mailgun_domain = None
_default_name = unicode("")
_default_email = unicode("")
_new_comment_subject = unicode("A comment on task ")
_BOT_EMAIL = get_unicode(unicode("BOT_EMAIL"))
class _Mail(object):
"""Mail constants for interacting with incoming mail from MailGun.
http://documentation.mailgun.net/user_manual.html#receiving-messages
"""
@constant
def RECIPIENT(self):
return "recipient"
@constant
def SENDER(self):
return "sender"
@constant
def FROM(self):
return "from"
@constant
def TO(self):
return "to"
@constant
def SUBJECT(self):
return "subject"
@constant
def BODY_TEXT(self):
return "body-plain"
@constant
def BODY_HTML(self):
return "body-html"
@constant
def BODY_TEXT_STRIPPED(self):
return "stripped-text"
@constant
def BODY_HTML_STRIPPED(self):
return "stripped-html"
@constant
def STRIPPED_SIGNATURE(self):
return "stripped-signature"
@constant
def ATTACHMENT_COUNT(self):
return "attachment-count"
@constant
def ATTACHMENT_X(self):
return "attachment-x"
@constant
def TIMESTAMP(self):
return "timestamp"
@constant
def TOKEN(self):
return "token"
@constant
def SIGNATURE(self):
return "signature"
@constant
def MESSAGE_HEADERS(self):
return "message-headers"
@constant
def CONTENT_ID_MAP(self):
return "content-id-map"
@constant
def TEXT(self):
return "text"
@constant
def API(self):
return "api"
MAIL = _Mail()
def initialize(api_key, domain, email, name=None):
"""Set the api key and domain for the mailgun account to use."""
global _mailgun_api_key
global _mailgun_domain
global _default_name
global _default_email
_mailgun_api_key = api_key
_mailgun_domain = domain
_default_email = email
if name:
_default_name = name
else:
_default_name = _get_default_sender_name()
def send_comment_on_task(service, task_id, recipient, message):
"""Send an smtp email for a task using Mailgun's API.
Parameters
----------
service : str
task_id : id
recipient : str
Formatted as "Name <email@domain.com>"
message : str
"""
from_email = unicode("{}-{}-comment@{}").format(
service,
task_id,
_mailgun_domain)
subject = unicode("{} {}.").format(_new_comment_subject, task_id)
send_email_as_jack(
from_email,
recipient,
subject,
message)
def send_email_to_customer(customer, subject, body_text):
"""Send email to customer from Jackalope."""
recipient = _format_email_with_name(customer.full_name, customer.email)
return send_email_from_jack(recipient, subject, body_text)
def send_internal_email_from_service(service, id, subject, body_text):
"""Send internal email from a service."""
recipient = unicode("{}-{}@{}").format(service, id, _mailgun_domain)
return send_email_from_jack(recipient, subject, body_text)
def send_email_to_notification_account(notification_type, subject, body_text):
"""Send internal email to our notification account, which can be used to
alert us of an action."""
recipient = _BOT_EMAIL
updated_subject = unicode("{}{} - {}").format(
_get_default_subject_prefix(),
notification_type,
subject)
return send_email_from_jack(recipient, updated_subject, body_text)
def send_email_from_jack(recipient, subject, body):
"""Send a smtp email using Mailgun's API with 'Jack Lope' as the sender
and return the response dict.
Parameters
----------
recipient : `str`
Formatted as "Name <email@domain.com>"
subject : `str`
body : `str`
"""
return send_email(_get_default_sender(), recipient, subject, body)
def send_email_as_jack(sender_email, recipient, subject, body):
"""Send a smtp email using Mailgun's API with 'Jack Lope' as
the name and the sender_email as the email, and return the response dict.
Parameters
----------
sender_email `str`
recipient : `str`
Formatted as "Name <email@domain.com>"
subject : `str`
body : `str`
"""
sender = _format_email_with_name(_default_name, sender_email)
return send_email(sender, recipient, subject, body)
def send_email(sender, recipient, subject, body):
"""Send a smtp email using Mailgun's API and return the response dict.
Parameters
----------
sender : `str`
Formatted as "Name <email@domain.com>"
recipient : `str`
Formatted as "Name <email@domain.com>"
subject : `str`
body : `str`
"""
# TODO: Check for empty body_text or body_html as mailgun will throw an
# error.
data_dict = {
MAIL.FROM: sender,
MAIL.TO: [recipient],
MAIL.SUBJECT: subject,
MAIL.TEXT: body
}
url = unicode("{}/{}/{}").format(
MAILGUN_API_URL,
_mailgun_domain,
MAILGUN_MESSAGES_SUFFIX)
response = requests.post(
url,
auth=(MAIL.API, _mailgun_api_key),
data=data_dict)
return response.text
def _get_default_sender():
return _format_email_with_name(_default_name, _default_email)
def _format_email_with_name(name, email):
return unicode("{} <{}>").format(name, email)
def _get_default_sender_name():
name = ""
if Deployment.is_prod():
name = "Jackalope"
elif Deployment.is_staging():
name = "Jackalope Staging"
elif Deployment.is_dev():
name = "Jackalope Dev"
return unicode(name)
def _get_default_subject_prefix():
prefix = ""
if Deployment.is_prod():
prefix = "PROD: "
elif Deployment.is_staging():
prefix = "STAGING: "
elif Deployment.is_dev():
prefix = "DEV: "
return unicode(prefix)