def drain_mail_queue(self): # mailer doesn't play well with default mail testing mails = [] for m in email_engine.prioritize(): mails.append(m) m.delete() return mails
def test_mailer_priority(self): global sent_messages from mailer import send_mail with self.settings( MAILER_EMAIL_BACKEND="mailer.tests.TestMailerEmailBackend"): send_mail("Medium email", "Body", "*****@*****.**", [ "*****@*****.**", ], priority="medium") send_mail("Low email", "Body", "*****@*****.**", [ "*****@*****.**", ], priority="low") send_mail("High email", "Body", "*****@*****.**", [ "*****@*****.**", ], priority="high") self.assertEqual(Message.objects.count(), 3) self.assertEqual(len(sent_messages), 0) msg_objects = prioritize(2) self.assertEqual(len(msg_objects), 2) msg_objects = prioritize(4) self.assertEqual(len(msg_objects), 3) msg_objects = prioritize(-1) self.assertEqual(len(msg_objects), 3) self.assertEqual(msg_objects[0].subject, "High email") self.assertEqual(msg_objects[1].subject, "Medium email") self.assertEqual(msg_objects[2].subject, "Low email") msg_objects[1].status = STATUS_SENT msg_objects[1].save() msg_objects[2].status = STATUS_DEFERRED msg_objects[2].save() msg_objects = prioritize(-1) self.assertEqual(len(msg_objects), 1) self.assertEqual(msg_objects[0].subject, "High email")
def test_prioritize(self): with self.settings(MAILER_EMAIL_BACKEND= "django.core.mail.backends.locmem.EmailBackend"): mailer.send_mail("Subject", "Body", "*****@*****.**", ["*****@*****.**"], priority=PRIORITY_HIGH) mailer.send_mail("Subject", "Body", "*****@*****.**", ["*****@*****.**"], priority=PRIORITY_MEDIUM) mailer.send_mail("Subject", "Body", "*****@*****.**", ["*****@*****.**"], priority=PRIORITY_LOW) mailer.send_mail("Subject", "Body", "*****@*****.**", ["*****@*****.**"], priority=PRIORITY_HIGH) mailer.send_mail("Subject", "Body", "*****@*****.**", ["*****@*****.**"], priority=PRIORITY_HIGH) mailer.send_mail("Subject", "Body", "*****@*****.**", ["*****@*****.**"], priority=PRIORITY_LOW) mailer.send_mail("Subject", "Body", "*****@*****.**", ["*****@*****.**"], priority=PRIORITY_LOW) mailer.send_mail("Subject", "Body", "*****@*****.**", ["*****@*****.**"], priority=PRIORITY_MEDIUM) mailer.send_mail("Subject", "Body", "*****@*****.**", ["*****@*****.**"], priority=PRIORITY_MEDIUM) mailer.send_mail("Subject", "Body", "*****@*****.**", ["*****@*****.**"], priority=PRIORITY_LOW) mailer.send_mail("Subject", "Body", "*****@*****.**", ["*****@*****.**"], priority=PRIORITY_MEDIUM) mailer.send_mail("Subject", "Body", "*****@*****.**", ["*****@*****.**"], priority=PRIORITY_HIGH) mailer.send_mail("Subject", "Body", "*****@*****.**", ["*****@*****.**"], priority=PRIORITY_DEFERRED) self.assertEqual(Message.objects.count(), 13) self.assertEqual(Message.objects.deferred().count(), 1) self.assertEqual(Message.objects.non_deferred().count(), 12) messages = iter(engine.prioritize()) # High priority msg = next(messages) self.assertEqual(msg.email.from_email, "*****@*****.**") msg.delete() msg = next(messages) self.assertEqual(msg.email.from_email, "*****@*****.**") msg.delete() msg = next(messages) self.assertEqual(msg.email.from_email, "*****@*****.**") msg.delete() msg = next(messages) self.assertEqual(msg.email.from_email, "*****@*****.**") msg.delete() # Medium priority msg = next(messages) self.assertEqual(msg.email.from_email, "*****@*****.**") msg.delete() msg = next(messages) self.assertEqual(msg.email.from_email, "*****@*****.**") msg.delete() msg = next(messages) self.assertEqual(msg.email.from_email, "*****@*****.**") msg.delete() msg = next(messages) self.assertEqual(msg.email.from_email, "*****@*****.**") msg.delete() # Low priority msg = next(messages) self.assertEqual(msg.email.from_email, "*****@*****.**") msg.delete() msg = next(messages) self.assertEqual(msg.email.from_email, "*****@*****.**") msg.delete() msg = next(messages) self.assertEqual(msg.email.from_email, "*****@*****.**") msg.delete() msg = next(messages) self.assertEqual(msg.email.from_email, "*****@*****.**") msg.delete() # Ensure nothing else comes up self.assertRaises(StopIteration, lambda: next(messages)) # Ensure deferred was not deleted self.assertEqual(Message.objects.count(), 1) self.assertEqual(Message.objects.deferred().count(), 1)
def test_prioritize(self): with self.settings(MAILER_EMAIL_BACKEND="django.core.mail.backends.locmem.EmailBackend"): mailer.send_mail("Subject", "Body", "*****@*****.**", ["*****@*****.**"], "high") mailer.send_mail("Subject", "Body", "*****@*****.**", ["*****@*****.**"], "medium") mailer.send_mail("Subject", "Body", "*****@*****.**", ["*****@*****.**"], "low") mailer.send_mail("Subject", "Body", "*****@*****.**", ["*****@*****.**"], "high") mailer.send_mail("Subject", "Body", "*****@*****.**", ["*****@*****.**"], "high") mailer.send_mail("Subject", "Body", "*****@*****.**", ["*****@*****.**"], "low") mailer.send_mail("Subject", "Body", "*****@*****.**", ["*****@*****.**"], "low") mailer.send_mail("Subject", "Body", "*****@*****.**", ["*****@*****.**"], "medium") mailer.send_mail("Subject", "Body", "*****@*****.**", ["*****@*****.**"], "medium") mailer.send_mail("Subject", "Body", "*****@*****.**", ["*****@*****.**"], "low") mailer.send_mail("Subject", "Body", "*****@*****.**", ["*****@*****.**"], "medium") mailer.send_mail("Subject", "Body", "*****@*****.**", ["*****@*****.**"], "high") mailer.send_mail("Subject", "Body", "*****@*****.**", ["*****@*****.**"], "deferred") self.assertEqual(Message.objects.count(), 13) self.assertEqual(Message.objects.deferred().count(), 1) messages = engine.prioritize() # High priority msg = next(messages) self.assertEqual(msg.email.from_email, "*****@*****.**") msg.delete() msg = next(messages) self.assertEqual(msg.email.from_email, "*****@*****.**") msg.delete() msg = next(messages) self.assertEqual(msg.email.from_email, "*****@*****.**") msg.delete() msg = next(messages) self.assertEqual(msg.email.from_email, "*****@*****.**") msg.delete() # Medium priority msg = next(messages) self.assertEqual(msg.email.from_email, "*****@*****.**") msg.delete() msg = next(messages) self.assertEqual(msg.email.from_email, "*****@*****.**") msg.delete() msg = next(messages) self.assertEqual(msg.email.from_email, "*****@*****.**") msg.delete() msg = next(messages) self.assertEqual(msg.email.from_email, "*****@*****.**") msg.delete() # Low priority msg = next(messages) self.assertEqual(msg.email.from_email, "*****@*****.**") msg.delete() msg = next(messages) self.assertEqual(msg.email.from_email, "*****@*****.**") msg.delete() msg = next(messages) self.assertEqual(msg.email.from_email, "*****@*****.**") msg.delete() msg = next(messages) self.assertEqual(msg.email.from_email, "*****@*****.**") msg.delete() # Add one more mail that should still get delivered mailer.send_mail("Subject", "Body", "*****@*****.**", ["*****@*****.**"], "high") msg = next(messages) self.assertEqual(msg.email.from_email, "*****@*****.**") msg.delete() # Ensure nothing else comes up self.assertRaises(StopIteration, lambda: next(messages)) # Ensure deferred was not deleted self.assertEqual(Message.objects.count(), 1) self.assertEqual(Message.objects.deferred().count(), 1)
def send_all_throttled(): """ Send all eligible messages in the queue. Source: https://github.com/pinax/django-mailer/blob/master/mailer/engine.py This customized version will send only X mails per run. """ # The actual backend to use for sending, defaulting to the Django default. # To make testing easier this is not stored at module level. EMAIL_BACKEND = getattr(settings, 'MAILER_EMAIL_BACKEND', 'django.core.mail.backends.smtp.EmailBackend') lock = FileLock('send_mail') logging.debug("acquiring lock...") try: lock.acquire(LOCK_WAIT_TIMEOUT) except AlreadyLocked: logging.debug("lock already in place. quitting.") return except LockTimeout: logging.debug("waiting for the lock timed out. quitting.") return logging.debug("acquired.") start_time = time.time() deferred = 0 sent = 0 try: connection = None for message in prioritize(): try: if connection is None: connection = get_connection(backend=EMAIL_BACKEND) logging.info( "sending message '%s' to %s" % (message.subject.encode("utf-8"), u", ".join( message.to_addresses).encode("utf-8"))) # NOQA email = message.email email.connection = connection email.send() MessageLog.objects.log(message, 1) message.delete() sent += 1 except (socket_error, smtplib.SMTPSenderRefused, smtplib.SMTPRecipientsRefused, smtplib.SMTPAuthenticationError) as err: # NOQA message.defer() logging.info("message deferred due to failure: %s" % err) MessageLog.objects.log(message, 3, log_message=str(err)) deferred += 1 # Get new connection, it case the connection itself has an # error. connection = None if sent == getattr(settings, 'MAILER_THROTTLE_AMOUNT', 25): break finally: logging.debug("releasing lock...") lock.release() logging.debug("released.") logging.info("") logging.info("%s sent; %s deferred;" % (sent, deferred)) logging.info("done in %.2f seconds" % (time.time() - start_time))