diff options
author | Alexander Sulfrian <alexander@sulfrian.net> | 2016-01-23 19:24:42 +0100 |
---|---|---|
committer | Alexander Sulfrian <alexander@sulfrian.net> | 2016-01-25 01:56:48 +0100 |
commit | ea3983d891bc6e34a827902ac8cf15734923e14c (patch) | |
tree | 87abdc6d4c5976341b3d12ddc94af7a55e7c7d95 /accounts/backend | |
parent | 64ab1d6c61905df0c1c81ca8b7fb2135bb5692ee (diff) | |
download | web-ea3983d891bc6e34a827902ac8cf15734923e14c.tar.gz web-ea3983d891bc6e34a827902ac8cf15734923e14c.tar.bz2 web-ea3983d891bc6e34a827902ac8cf15734923e14c.zip |
backend/mail: Allow different backends for mail
Diffstat (limited to 'accounts/backend')
-rw-r--r-- | accounts/backend/__init__.py | 0 | ||||
-rw-r--r-- | accounts/backend/mail/__init__.py | 9 | ||||
-rw-r--r-- | accounts/backend/mail/dummy.py | 48 | ||||
-rw-r--r-- | accounts/backend/mail/sendmail.py | 35 |
4 files changed, 92 insertions, 0 deletions
diff --git a/accounts/backend/__init__.py b/accounts/backend/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/accounts/backend/__init__.py diff --git a/accounts/backend/mail/__init__.py b/accounts/backend/mail/__init__.py new file mode 100644 index 0000000..c22b037 --- /dev/null +++ b/accounts/backend/mail/__init__.py @@ -0,0 +1,9 @@ +# -*- coding: utf-8 -*- + +class Backend(object): + + def __init__(self, app): + self.app = app + + def send(self, recipient, subject, body, sender=None): + raise NotImplementedError() diff --git a/accounts/backend/mail/dummy.py b/accounts/backend/mail/dummy.py new file mode 100644 index 0000000..d62a66b --- /dev/null +++ b/accounts/backend/mail/dummy.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- +from __future__ import absolute_import + +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, subject, body, sender=None): + if sender is None: + sender = self.app.config['MAIL_DEFAULT_SENDER'] + + 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))) diff --git a/accounts/backend/mail/sendmail.py b/accounts/backend/mail/sendmail.py new file mode 100644 index 0000000..92b354e --- /dev/null +++ b/accounts/backend/mail/sendmail.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +from __future__ import absolute_import + +import subprocess +from email.mime.text import MIMEText +from email.utils import parseaddr + +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'] + + safe = lambda s: s.split('\n', 1)[0] + + msg = MIMEText(body, _charset='utf-8') + msg['Subject'] = safe(subject) + msg['To'] = safe(recipient) + msg['From'] = safe(sender) + + envelope = [] + _, address = parseaddr(safe(sender)) + if address != '': + envelope = ['-f', address] + + p = subprocess.Popen([self.app.config['SENDMAIL_COMMAND']] + + envelope + ['-t'], stdin=subprocess.PIPE) + p.stdin.write(msg.as_string()) + p.stdin.close() + + if p.wait() != 0: + raise RuntimeError('sendmail terminated with %d' % p.returncode) |