summaryrefslogtreecommitdiffstats
path: root/accounts/backend/mail
diff options
context:
space:
mode:
Diffstat (limited to 'accounts/backend/mail')
-rw-r--r--accounts/backend/mail/__init__.py17
-rw-r--r--accounts/backend/mail/dummy.py24
-rw-r--r--accounts/backend/mail/sendmail.py17
3 files changed, 37 insertions, 21 deletions
diff --git a/accounts/backend/mail/__init__.py b/accounts/backend/mail/__init__.py
index ecd0d45..f2ec5d5 100644
--- a/accounts/backend/mail/__init__.py
+++ b/accounts/backend/mail/__init__.py
@@ -1,18 +1,25 @@
# -*- coding: utf-8 -*-
-class Backend(object):
+from flask.app import Flask
+from jinja2 import Template
+from jinja2.environment import TemplateModule
- def __init__(self, app):
+
+class Backend():
+ app: Flask
+
+ def __init__(self, app: Flask) -> None:
self.app = app
- def _send(self, recipient, content):
+ def _send(self, recipient: str, content: TemplateModule):
raise NotImplementedError()
- def send(self, recipient, template, **kwargs):
+ def send(self, recipient: str, template, **kwargs):
if recipient is None:
return
- tmpl = self.app.jinja_env.get_or_select_template(template)
+ tmpl: Template = \
+ self.app.jinja_env.get_or_select_template(template)
kwargs['recipient'] = recipient
module = tmpl.make_module(kwargs)
diff --git a/accounts/backend/mail/dummy.py b/accounts/backend/mail/dummy.py
index 61e7b75..3de3dbe 100644
--- a/accounts/backend/mail/dummy.py
+++ b/accounts/backend/mail/dummy.py
@@ -2,7 +2,8 @@
from . import Backend
-from accounts.utils import ensure_utf8
+from accounts import AccountsFlask
+from jinja2.environment import TemplateModule
FANCY_FORMAT = '''\
@@ -23,8 +24,10 @@ From: {sender}
class DummyBackend(Backend):
+ format: str
- def __init__(self, app, plain=False, format=None):
+ def __init__(self, app: AccountsFlask, plain: bool = False,
+ format: None = None) -> None:
super(DummyBackend, self).__init__(app)
self.plain = plain
@@ -36,13 +39,16 @@ class DummyBackend(Backend):
else:
self.format = format
- def _send(self, recipient, content):
- body = content.body()
+ def _send(self, recipient: str, content: TemplateModule):
+ print(type(content))
+
+ # 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=ensure_utf8(content.subject()),
- sender=ensure_utf8(content.sender()),
- to=ensure_utf8(recipient),
- body=ensure_utf8(body))))
+ print(self.format.format(
+ subject=content.subject(), # type: ignore
+ sender=content.sender(), # type: ignore
+ to=recipient,
+ body=body))
diff --git a/accounts/backend/mail/sendmail.py b/accounts/backend/mail/sendmail.py
index 5ac5d93..1fecedc 100644
--- a/accounts/backend/mail/sendmail.py
+++ b/accounts/backend/mail/sendmail.py
@@ -4,27 +4,30 @@
import subprocess
from email.mime.text import MIMEText
from email.utils import parseaddr
+from jinja2.environment import TemplateModule
from . import Backend
-class SendmailBackend(Backend):
+def safe(s: str):
+ return s.split('\n', 1)[0]
- def _send(self, recipient, content):
- safe = lambda s: s.split('\n', 1)[0]
- msg = MIMEText(content.body(), _charset='utf-8')
- msg['Subject'] = safe(content.subject())
+class SendmailBackend(Backend):
+ def _send(self, recipient: str, content: TemplateModule):
+ msg = MIMEText(content.body(), _charset='utf-8') # type: ignore
+ msg['Subject'] = safe(content.subject()) # type: ignore
msg['To'] = safe(recipient)
- msg['From'] = safe(content.sender())
+ msg['From'] = safe(content.sender()) # type: ignore
envelope = []
- _, address = parseaddr(safe(content.sender()))
+ _, address = parseaddr(safe(content.sender())) # type: ignore
if address != '':
envelope = ['-f', address]
p = subprocess.Popen([self.app.config['SENDMAIL_COMMAND']] +
envelope + ['-t'], stdin=subprocess.PIPE)
+ assert p.stdin
p.stdin.write(msg.as_string().encode("utf-8"))
p.stdin.close()