This repository was archived by the owner on Dec 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase.py
158 lines (129 loc) · 4.94 KB
/
base.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
from __future__ import unicode_literals
from cbmail.conf import settings
from django.conf import settings as dj_settings
from django.core.mail import get_connection
from django.core.mail.message import EmailMultiAlternatives
from django.template import loader
from django.utils.html import strip_tags
class BaseMail(object):
"""
This class represents the base of a templated email
TODO: add prefix support to subject
"""
template_name = None
mail_cc = []
mail_bcc = []
mail_reply_to = []
mail_from = None
subject = None
def get_attachments(self):
""" Return a list of Attachment instances """
return []
def get_context_data(self):
""" Return a dictionary with context to be used on template """
return {}
@staticmethod
def _filter_whitelist(mails):
whitelist = settings.WHITELIST
if whitelist:
return list(set(mails) & set(whitelist))
return mails
def get_mail_to(self, object_or_list):
"""
Returns the list of emails to be used on to email field
objects_or_list can be a list of emails or a object instance
with the get_mailing_list method defined
"""
if isinstance(object_or_list, object) and\
hasattr(object_or_list, 'get_mailing_list'):
object_or_list = object_or_list.get_mailing_list()
elif not isinstance(object_or_list, list):
raise ValueError(
'object_or_list must be object with get_mailing_list method '
'defined or list instance.')
return BaseMail._filter_whitelist(object_or_list)
def get_mail_cc(self):
""" Returns the list of emails to be used on cc email field """
return self.mail_cc
def get_mail_bcc(self):
""" Returns the list of emails to be used on bcc email field """
return self.mail_bcc
def get_mail_reply_to(self):
"""
Returns the email to be used on the reply to email field
If one is not provided it will get the DEFAULT_REPLY_TO setting
"""
if self.mail_reply_to:
return self.mail_reply_to
return settings.DEFAULT_REPLY_TO
def get_mail_from(self):
"""
Returns the email to be used on the from email field
If one is not provided it will get the Django default email setting
"""
if self.mail_from:
return self.mail_from
return dj_settings.DEFAULT_FROM_EMAIL
def get_subject(self):
"""
Returns the subject to be used on the subject email field
If one is not provided it will get the DEFAULT_SUBJECT setting
"""
if self.subject:
return self.subject
return settings.DEFAULT_SUBJECT
def get_template_name(self):
""" Returns the template name to be used to render the email """
return self.template_name
def get_base_url(self):
""" Returns the base url to be used on the email """
return settings.BASE_URL
def render(self):
""" Return the body of the email """
context = self.get_context_data()
context.update(settings.EXTRA_DATA)
context.update({
'base_url': self.get_base_url(),
'subject': self.get_subject()
})
html_as_string = loader.render_to_string(
self.get_template_name(), context)
return html_as_string
def send(self, object_or_list):
"""
Given an object_or_list creates a EmailMultiAlternatives and
send it to the respective destination.
If Attachments exist, also adds them to the messsage.
"""
html_as_string = self.render()
text_part = strip_tags(html_as_string)
to = self.get_mail_to(object_or_list)
if to:
msg = EmailMultiAlternatives(
self.get_subject(),
text_part,
self.get_mail_from(),
to=to,
cc=self.get_mail_cc(),
bcc=self.get_mail_bcc(),
reply_to=self.get_mail_reply_to())
# Attach the html version of email
msg.attach_alternative(html_as_string, "text/html")
# If there is attachments attach them to the email
for attachment in self.get_attachments():
msg.attach(*attachment.get_triple())
return get_connection().send_messages([msg])
return 0
class Attachment(object):
""" This class represents one email attachment """
filename = None
contents = None
mimetype = None
def __init__(self, filename, contents, mimetype=None, *args, **kwargs):
self.filename = filename
self.contents = contents
self.mimetype = mimetype
super(Attachment, self).__init__(*args, **kwargs)
def get_triple(self):
""" Returns the triple to be user when attached to message """
return self.filename, self.contents, self.mimetype