summaryrefslogtreecommitdiffstats
path: root/accounts/backend/mail/dummy.py
blob: 0c6da21c709e312a440dbb656aa204c83b63337e (plain)
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
# -*- coding: utf-8 -*-


from . import Backend
from accounts import AccountsFlask
from jinja2.environment import TemplateModule


FANCY_FORMAT = """\
,---------------------------------------------------------------------------
| Subject: {subject}
| To: {to}
| From: {sender}
|---------------------------------------------------------------------------
| {body}
`---------------------------------------------------------------------------"""


PLAIN_FORMAT = """Subject: {subject}
To: {to}
From: {sender}

{body}"""


class DummyBackend(Backend):
    format: str

    def __init__(
        self, app: AccountsFlask, plain: bool = False, format: None = None
    ) -> None:
        super(DummyBackend, self).__init__(app)
        self.plain = plain

        if format is None:
            if self.plain:
                self.format = PLAIN_FORMAT
            else:
                self.format = FANCY_FORMAT
        else:
            self.format = format

    def _send(self, recipient: str, content: TemplateModule):
        # we can't typecheck things defined in templates
        body = content.body()  # type: ignore
        if not self.plain:
            body = "\n| ".join(body.split("\n"))

        print(
            self.format.format(
                subject=content.subject(),  # type: ignore
                sender=content.sender(),  # type: ignore
                to=recipient,
                body=body,
            )
        )