diff options
Diffstat (limited to 'accounts/backend/mail')
-rw-r--r-- | accounts/backend/mail/__init__.py | 7 | ||||
-rw-r--r-- | accounts/backend/mail/dummy.py | 26 | ||||
-rw-r--r-- | accounts/backend/mail/sendmail.py | 22 |
3 files changed, 30 insertions, 25 deletions
diff --git a/accounts/backend/mail/__init__.py b/accounts/backend/mail/__init__.py index f2ec5d5..bb1709f 100644 --- a/accounts/backend/mail/__init__.py +++ b/accounts/backend/mail/__init__.py @@ -5,7 +5,7 @@ from jinja2 import Template from jinja2.environment import TemplateModule -class Backend(): +class Backend: app: Flask def __init__(self, app: Flask) -> None: @@ -18,10 +18,9 @@ class Backend(): if recipient is None: return - tmpl: Template = \ - self.app.jinja_env.get_or_select_template(template) + tmpl: Template = self.app.jinja_env.get_or_select_template(template) - kwargs['recipient'] = recipient + 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 049df7a..0c6da21 100644 --- a/accounts/backend/mail/dummy.py +++ b/accounts/backend/mail/dummy.py @@ -6,28 +6,29 @@ from accounts import AccountsFlask from jinja2.environment import TemplateModule -FANCY_FORMAT = '''\ +FANCY_FORMAT = """\ ,--------------------------------------------------------------------------- | Subject: {subject} | To: {to} | From: {sender} |--------------------------------------------------------------------------- | {body} -`---------------------------------------------------------------------------''' +`---------------------------------------------------------------------------""" -PLAIN_FORMAT = '''Subject: {subject} +PLAIN_FORMAT = """Subject: {subject} To: {to} From: {sender} -{body}''' +{body}""" class DummyBackend(Backend): format: str - def __init__(self, app: AccountsFlask, plain: bool = False, - format: None = None) -> None: + def __init__( + self, app: AccountsFlask, plain: bool = False, format: None = None + ) -> None: super(DummyBackend, self).__init__(app) self.plain = plain @@ -45,8 +46,11 @@ class DummyBackend(Backend): 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)) + 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 1fecedc..abaab87 100644 --- a/accounts/backend/mail/sendmail.py +++ b/accounts/backend/mail/sendmail.py @@ -10,26 +10,28 @@ from . import Backend def safe(s: str): - return s.split('\n', 1)[0] + return s.split("\n", 1)[0] 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()) # type: ignore + 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()) # type: ignore envelope = [] _, address = parseaddr(safe(content.sender())) # type: ignore - if address != '': - envelope = ['-f', address] + if address != "": + envelope = ["-f", address] - p = subprocess.Popen([self.app.config['SENDMAIL_COMMAND']] + - envelope + ['-t'], stdin=subprocess.PIPE) + 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() if p.wait() != 0: - raise RuntimeError('sendmail terminated with %d' % p.returncode) + raise RuntimeError("sendmail terminated with %d" % p.returncode) |