From e5542372e962e85dbebe961200c9b51b2238b5cc Mon Sep 17 00:00:00 2001 From: "Pavel \"LEdoian\" Turinsky" Date: Tue, 25 May 2021 19:58:56 +0200 Subject: [PATCH] =?UTF-8?q?Monkeypatch=20mailov=C3=A9ho=20backendu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- various/mail_prefixer.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 various/mail_prefixer.py diff --git a/various/mail_prefixer.py b/various/mail_prefixer.py new file mode 100644 index 00000000..b4597ac9 --- /dev/null +++ b/various/mail_prefixer.py @@ -0,0 +1,16 @@ +"""A simple email backend, which only prepends all subjects with a string. + +Used to distinguish testing emails from production ones.""" + +# We try to monkeypatch django.core.mail.backends.smtp :-) + +from djago.core.mail.backends.smtp import EmailBackend as DjangoSMTPBackend + +class PrefixingMailBackend(DjangoSMTPBackend): + # method _send is not probably meant to be monkey_patched, so we patch send_messages instead. + def send_messages(self, messages): + prefix = '[Mail z testwebu]' + for message in messages: + # We hope that this is a django.core.mail.message.EmailMessage + message.subject = prefix + ' ' + message.subject + return super().send_messages(messages)