summaryrefslogtreecommitdiffstats
path: root/accounts/backend/mail
diff options
context:
space:
mode:
authorAlexander Sulfrian <alexander@sulfrian.net>2016-02-02 03:07:44 +0100
committerAlexander Sulfrian <alexander@sulfrian.net>2016-02-02 04:44:07 +0100
commit9a196839970e7d98a2bd9375bbd470846ccb3a27 (patch)
tree1c52403114bb5da11db863da72d67f2b6d75ac98 /accounts/backend/mail
parent34b571a7335ba3d2b71409068b93df603b0271b9 (diff)
downloadweb-9a196839970e7d98a2bd9375bbd470846ccb3a27.tar.gz
web-9a196839970e7d98a2bd9375bbd470846ccb3a27.tar.bz2
web-9a196839970e7d98a2bd9375bbd470846ccb3a27.zip
templates/mail: Get all parts from the template
Also render sender and subject with the mail template, so the mails can be created with only a template name, a recipient and the template args. The required confirmation links are also generated in the templates.
Diffstat (limited to 'accounts/backend/mail')
-rw-r--r--accounts/backend/mail/__init__.py13
-rw-r--r--accounts/backend/mail/dummy.py12
-rw-r--r--accounts/backend/mail/sendmail.py13
3 files changed, 23 insertions, 15 deletions
diff --git a/accounts/backend/mail/__init__.py b/accounts/backend/mail/__init__.py
index c22b037..ecd0d45 100644
--- a/accounts/backend/mail/__init__.py
+++ b/accounts/backend/mail/__init__.py
@@ -5,5 +5,16 @@ class Backend(object):
def __init__(self, app):
self.app = app
- def send(self, recipient, subject, body, sender=None):
+ def _send(self, recipient, content):
raise NotImplementedError()
+
+ def send(self, recipient, template, **kwargs):
+ if recipient is None:
+ return
+
+ tmpl = self.app.jinja_env.get_or_select_template(template)
+
+ kwargs['recipient'] = recipient
+ module = tmpl.make_module(kwargs)
+
+ self._send(recipient, module)
diff --git a/accounts/backend/mail/dummy.py b/accounts/backend/mail/dummy.py
index d62a66b..ad38d42 100644
--- a/accounts/backend/mail/dummy.py
+++ b/accounts/backend/mail/dummy.py
@@ -36,13 +36,13 @@ class DummyBackend(Backend):
else:
self.format = format
- def send(self, recipient, subject, body, sender=None):
- if sender is None:
- sender = self.app.config['MAIL_DEFAULT_SENDER']
-
+ 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(subject), to=ensure_utf8(recipient),
- sender=ensure_utf8(sender), body=ensure_utf8(body)))
+ subject=ensure_utf8(content.subject()),
+ sender=ensure_utf8(content.sender()),
+ to=ensure_utf8(recipient),
+ body=ensure_utf8(body)))
diff --git a/accounts/backend/mail/sendmail.py b/accounts/backend/mail/sendmail.py
index 92b354e..9a39b03 100644
--- a/accounts/backend/mail/sendmail.py
+++ b/accounts/backend/mail/sendmail.py
@@ -10,19 +10,16 @@ from . import Backend
class SendmailBackend(Backend):
- def send(self, recipient, subject, body, sender=None):
- if sender is None:
- sender = self.app.config['MAIL_DEFAULT_SENDER']
-
+ def _send(self, recipient, content):
safe = lambda s: s.split('\n', 1)[0]
- msg = MIMEText(body, _charset='utf-8')
- msg['Subject'] = safe(subject)
+ msg = MIMEText(content.body(), _charset='utf-8')
+ msg['Subject'] = safe(content.subject())
msg['To'] = safe(recipient)
- msg['From'] = safe(sender)
+ msg['From'] = safe(content.sender())
envelope = []
- _, address = parseaddr(safe(sender))
+ _, address = parseaddr(safe(content.sender()))
if address != '':
envelope = ['-f', address]