-
Notifications
You must be signed in to change notification settings - Fork 130
/
__init__.py
62 lines (54 loc) · 2.9 KB
/
__init__.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
from django.conf import settings
from django.utils.importlib import import_module
from django.core.exceptions import ImproperlyConfigured
from templated_email.backends.vanilla_django import TemplateBackend
def get_connection(backend=None, template_prefix=None,
fail_silently=False, **kwargs):
"""Load a templated e-mail backend and return an instance of it.
If backend is None (default) settings.TEMPLATED_EMAIL_BACKEND is used.
Both fail_silently and other keyword arguments are used in the
constructor of the backend.
"""
# This method is mostly a copy of the backend loader present in django.core.mail.get_connection
path = backend or getattr(settings,'TEMPLATED_EMAIL_BACKEND','templated_email.backends.vanilla_django.TemplateBackend')
try:
# First check if class name is omited and we have module in settings
mod = import_module(path)
klass_name = 'TemplateBackend'
except ImportError, e:
# Fallback to class name
try:
mod_name, klass_name = path.rsplit('.', 1)
mod = import_module(mod_name)
except ImportError, e:
raise ImproperlyConfigured(
('Error importing templated email backend module %s: "%s"'
% (mod_name, e)))
try:
klass = getattr(mod, klass_name)
except AttributeError:
raise ImproperlyConfigured(('Module "%s" does not define a '
'"%s" class' % (mod_name, klass_name)))
return klass(fail_silently=fail_silently, template_prefix=template_prefix,
**kwargs)
def get_templated_mail(template_name, context, from_email=None, to=None,
cc=None, bcc=None, headers=None, template_prefix=None,
template_dir=None):
"""Returns a templated EmailMessage instance without a connection using
the django templating backend."""
templater = TemplateBackend(template_prefix=template_prefix)
return templater.get_email_message(template_name, context,
from_email=from_email, to=to,
cc=cc, bcc=bcc, headers=headers,
template_dir=template_dir)
def send_templated_mail(template_name, from_email, recipient_list, context,
cc=None, bcc=None, fail_silently=False, connection=None,
headers=None, template_dir=None, **kwargs):
"""Easy wrapper for sending a templated email to a recipient list.
Final behaviour of sending depends on the currently selected engine.
See BackendClass.send.__doc__
"""
connection = connection or get_connection()
return connection.send(template_name, from_email, recipient_list, context,
cc=cc, bcc=bcc, fail_silently=fail_silently,
headers=headers, template_dir=template_dir, **kwargs)