1 | from django.core.mail.backends.base import BaseEmailBackend
|
---|
2 | from django.utils.encoding import force_unicode
|
---|
3 |
|
---|
4 | from mailer import PRIORITY_MAPPING
|
---|
5 | from mailer.models import Message
|
---|
6 |
|
---|
7 |
|
---|
8 | class EmailBackend(BaseEmailBackend):
|
---|
9 |
|
---|
10 | def __init__(self, *args, **kwds):
|
---|
11 | if 'priority' in kwds:
|
---|
12 | self.priority = kwds.pop('priority')
|
---|
13 | else:
|
---|
14 | self.priority = 'medium'
|
---|
15 | super(EmailBackend, self).__init__(*args, **kwds)
|
---|
16 |
|
---|
17 | def _store_message(self, message):
|
---|
18 | subject = force_unicode(message.subject)
|
---|
19 | priority = PRIORITY_MAPPING[self.priority]
|
---|
20 | for to_address in message.recipients():
|
---|
21 | Message(to_address=to_address,
|
---|
22 | from_address=message.from_email,
|
---|
23 | subject=subject,
|
---|
24 | message_body=message.body,
|
---|
25 | priority=priority).save()
|
---|
26 |
|
---|
27 | def send_messages(self, email_messages):
|
---|
28 | num_sent = 0
|
---|
29 | for message in email_messages:
|
---|
30 | try:
|
---|
31 | self._store_message(message)
|
---|
32 | num_sent += 1
|
---|
33 | except:
|
---|
34 | if not self.fail_silently:
|
---|
35 | raise
|
---|
36 | return num_sent
|
---|