SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit 09f42775 authored by Jorran de Wit's avatar Jorran de Wit
Browse files

Update mail db after sending email

parent 0b511f3d
No related branches found
No related tags found
No related merge requests found
......@@ -37,5 +37,5 @@ CROSSREF_LOGIN_PASSWORD = get_secret("CROSSREF_LOGIN_PASSWORD")
CROSSREF_DEPOSIT_EMAIL = 'jorrandewit@scipost.org'
# Customized mailbackend
EMAIL_BACKEND = 'mails.backends.filebased.EmailBackend'
EMAIL_BACKEND_ORIGINAL = 'django.core.mail.backends.filebased.EmailBackend'
EMAIL_BACKEND = 'mails.backends.filebased.ModelEmailBackend'
EMAIL_BACKEND_ORIGINAL = 'mails.backends.filebased.EmailBackend'
......@@ -12,6 +12,8 @@ class EmailBackend(FileBacked):
self.stream.write(b'Bcc: ' + bcc_str + b'\n')
super().write_message(message)
class ModelEmailBackend(FileBacked):
def send_messages(self, email_messages, force_original=False):
"""Write all messages to the stream in a thread-safe way."""
if force_original:
......
......@@ -19,7 +19,12 @@ class Command(BaseCommand):
if hasattr(settings, 'EMAIL_BACKEND_ORIGINAL'):
backend = settings.EMAIL_BACKEND_ORIGINAL
else:
# Fallback to Django's default
backend = 'django.core.mail.backends.smtp.EmailBackend'
if backend == 'mails.backends.filebased.ModelEmailBackend':
raise AssertionError('The `EMAIL_BACKEND_ORIGINAL` cannot be the ModelEmailBackend')
connection = get_connection(backend=backend, fail_silently=False)
for db_mail in mails:
mail = EmailMultiAlternatives(
......@@ -32,8 +37,11 @@ class Command(BaseCommand):
connection=connection)
if db_mail.body_html:
mail.attach_alternative(db_mail.body_html, 'text/html')
print('mail ' + str(db_mail.id))
return mail.send()
response = mail.send()
if response:
db_mail.processed = True
db_mail.save()
return response
def handle(self, *args, **options):
if options.get('id'):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment