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


from . import Backend
from accounts.utils import ensure_utf8


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


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

{body}'''


class DummyBackend(Backend):

    def __init__(self, app, plain=False, format=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, content):
        body = content.body()
        if not self.plain:
            body = "\n| ".join(body.split("\n"))

        print((self.format.format(
            subject=ensure_utf8(content.subject()),
            sender=ensure_utf8(content.sender()),
            to=ensure_utf8(recipient),
            body=ensure_utf8(body))))