diff --git a/post_office/backends.py b/post_office/backends.py index 00bfb3df..2622665e 100644 --- a/post_office/backends.py +++ b/post_office/backends.py @@ -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 @@ -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 diff --git a/post_office/mail.py b/post_office/mail.py index 3a9a95a1..e4a1c130 100644 --- a/post_office/mail.py +++ b/post_office/mail.py @@ -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 diff --git a/post_office/tests/test_backends.py b/post_office/tests/test_backends.py index 0ec944ab..32618078 100644 --- a/post_office/tests/test_backends.py +++ b/post_office/tests/test_backends.py @@ -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 @@ -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', 'from1@example.com', ['to@example.com']) + email = Email.objects.latest('id') + self.assertEqual(email.status, STATUS.queued) + self.assertEqual(mock.call_count, 1) diff --git a/post_office/utils.py b/post_office/utils.py index 73b0ac31..db69e233 100644 --- a/post_office/utils.py +++ b/post_office/utils.py @@ -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 @@ -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