File: __init__.py

package info (click to toggle)
django-templated-email 3.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 200 kB
  • sloc: python: 389; makefile: 4
file content (68 lines) | stat: -rw-r--r-- 3,280 bytes parent folder | download
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
from django.conf import settings
from django.utils.module_loading import import_string

from templated_email.backends.vanilla_django import TemplateBackend
from templated_email.utils import InlineImage  # noqa


def get_connection(backend=None, template_prefix=None, template_suffix=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
    klass_path = backend or getattr(settings, 'TEMPLATED_EMAIL_BACKEND',
                                    TemplateBackend)
    if isinstance(klass_path, str):
        try:
            # First check if class name is omitted and we have module in settings
            klass = import_string(klass_path + '.' + 'TemplateBackend')
        except ImportError:
            # Fallback to class name
            klass = import_string(klass_path)
    else:
        klass = klass_path

    return klass(fail_silently=fail_silently, template_prefix=template_prefix,
                 template_suffix=template_suffix, **kwargs)


def get_templated_mail(template_name, context, from_email=None, to=None,
                       cc=None, bcc=None, headers=None,
                       template_prefix=None, template_suffix=None,
                       template_dir=None, file_extension=None,
                       create_link=False):
    """Returns a templated EmailMessage instance without a connection using
    the django templating backend."""
    template_prefix = template_prefix or template_dir
    template_suffix = template_suffix or file_extension
    templater = TemplateBackend(template_prefix=template_prefix,
                                template_suffix=template_suffix)
    return templater.get_email_message(template_name, context,
                                       from_email=from_email, to=to,
                                       cc=cc, bcc=bcc, headers=headers,
                                       template_prefix=template_prefix,
                                       template_suffix=template_suffix,
                                       create_link=create_link)


def send_templated_mail(template_name, from_email, recipient_list, context,
                        cc=None, bcc=None, fail_silently=False, connection=None,
                        headers=None, template_prefix=None,
                        template_suffix=None,
                        create_link=False, **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(template_prefix=template_prefix,
                                              template_suffix=template_suffix)
    return connection.send(template_name, from_email, recipient_list, context,
                           cc=cc, bcc=bcc, fail_silently=fail_silently,
                           headers=headers, create_link=create_link, **kwargs)