mamweb/various/mail_prefixer.py

28 lines
988 B
Python
Raw Normal View History

2021-05-25 19:58:56 +02:00
"""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 :-)
2021-05-25 23:39:15 +02:00
from django.core.mail.backends.smtp import EmailBackend as DjangoSMTPBackend
from django.conf import settings
2021-05-25 19:58:56 +02:00
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
2021-05-25 23:49:39 +02:00
if message.from_email != settings.SERVER_EMAIL:
2021-05-25 23:49:39 +02:00
message.subject = prefix + ' ' + message.subject
message.body = f"""Bylo by posláno na e-maily:
To: {message.to}
Cc: {message.cc}
Bcc: {message.bcc}
"""+ "\n\n" + message.body
2021-05-25 23:49:39 +02:00
message.to = settings.TESTOVACI_EMAILOVA_KONFERENCE
message.cc = []
message.bcc = []
2021-05-25 19:58:56 +02:00
return super().send_messages(messages)