Skip to content

Commit

Permalink
Fix #435 (#469)
Browse files Browse the repository at this point in the history
Make sure to always send email_queued signal when creating emails with priority != now
  • Loading branch information
diesieben07 authored Jun 12, 2024
1 parent 12a68e5 commit 3a5801b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
13 changes: 11 additions & 2 deletions post_office/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ def send_messages(self, email_messages):
email messages sent.
"""
from .mail import create
from .models import STATUS
from .models import STATUS, Email
from .utils import create_attachments
from .signals import email_queued

if not email_messages:
return

default_priority = get_default_priority()
num_sent = 0
emails = []
for email_message in email_messages:
subject = email_message.subject
from_email = email_message.from_email
Expand Down Expand Up @@ -62,8 +65,14 @@ def send_messages(self, email_messages):

email.attachments.add(*attachments)

if get_default_priority() == 'now':
emails.append(email)

if default_priority == 'now':
status = email.dispatch()
if status == STATUS.sent:
num_sent += 1

if default_priority != 'now':
email_queued.send(sender=Email, emails=emails)

return num_sent
3 changes: 2 additions & 1 deletion post_office/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ def send(

if priority == PRIORITY.now:
email.dispatch(log_level=log_level)
email_queued.send(sender=Email, emails=[email])
elif commit:
email_queued.send(sender=Email, emails=[email])

return email

Expand Down
17 changes: 17 additions & 0 deletions post_office/tests/test_backends.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
from email.mime.image import MIMEImage
from unittest import mock

from django.conf import settings
from django.core.files.images import File
from django.core.mail import EmailMultiAlternatives, send_mail, EmailMessage
Expand Down Expand Up @@ -164,3 +166,18 @@ def test_default_priority_now(self):
email = Email.objects.latest('id')
self.assertEqual(email.status, STATUS.sent)
self.assertEqual(num_sent, 1)

@override_settings(
EMAIL_BACKEND='post_office.EmailBackend',
POST_OFFICE={
'DEFAULT_PRIORITY': 'medium',
'BACKENDS': {'default': 'django.core.mail.backends.dummy.EmailBackend'}
}
)
@mock.patch('post_office.signals.email_queued.send')
def test_email_queued_signal(self, mock):
# If DEFAULT_PRIORITY is not "now", the email_queued signal should be sent
send_mail('Test', 'Message', '[email protected]', ['[email protected]'])
email = Email.objects.latest('id')
self.assertEqual(email.status, STATUS.queued)
self.assertEqual(mock.call_count, 1)
3 changes: 3 additions & 0 deletions post_office/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from post_office import cache
from .models import Email, PRIORITY, STATUS, EmailTemplate, Attachment
from .settings import get_default_priority
from .signals import email_queued
from .validators import validate_email_with_name


Expand All @@ -28,6 +29,8 @@ def send_mail(subject, message, from_email, recipient_list, html_message='',
if priority == PRIORITY.now:
for email in emails:
email.dispatch()
else:
email_queued.send(sender=Email, emails=emails)
return emails


Expand Down

0 comments on commit 3a5801b

Please sign in to comment.