-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemailer.py
36 lines (30 loc) · 1.03 KB
/
emailer.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
import mandrill
from nameko.extensions import DependencyProvider
__all__ = ['Emailer','InvalidEmail','FailedSendingEmail']
class InvalidEmail(Exception):
pass
class FailedSendingEmail(Exception):
pass
class Emailer(DependencyProvider):
def sendEmail(self,sender,recipient,text,key):
# instanciate a new one every time to avoid possible
# concurrency issues.
m = mandrill.Mandrill(apikey=key)
email = dict(
text = text,
subject = 'Payment received',
from_email = sender,
from_name = 'Claude Gibert',
to = [ dict(email = recipient) ],
key = key,
)
try:
res = m.messages.send(email)
except mandrill.ValidationError as e:
raise InvalidEmail(e)
else:
result = res[0]
if not result['status'] == 'sent':
raise FailedSendingEmail('%s %s' % (result['status'],result['reject_reason']))
def get_dependency(self, worker_ctx):
return self