2020-06-15 00:02:39 +00:00
|
|
|
import logging
|
2020-04-30 07:43:10 +00:00
|
|
|
from smtplib import SMTPException
|
2020-06-12 04:24:24 +00:00
|
|
|
from threading import Thread
|
2020-04-30 07:43:10 +00:00
|
|
|
|
2020-06-15 05:04:48 +00:00
|
|
|
from django.db import close_old_connections, transaction
|
2020-04-30 07:43:10 +00:00
|
|
|
from django.template.loader import render_to_string
|
2020-06-12 04:24:24 +00:00
|
|
|
from django.utils import timezone
|
2020-04-30 07:43:10 +00:00
|
|
|
|
2020-06-15 00:02:39 +00:00
|
|
|
from birdsong.models import Campaign, CampaignStatus, Contact
|
2020-05-01 00:48:00 +00:00
|
|
|
from birdsong.utils import send_mass_html_mail
|
2020-06-15 00:02:39 +00:00
|
|
|
|
2020-04-30 07:43:10 +00:00
|
|
|
from . import BaseEmailBackend
|
|
|
|
|
2020-06-12 04:24:24 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2020-06-15 00:02:39 +00:00
|
|
|
|
2020-06-12 04:24:24 +00:00
|
|
|
class SendCampaignThread(Thread):
|
2020-06-15 04:20:55 +00:00
|
|
|
def __init__(self, campaign_pk, contact_pks, messages):
|
2020-06-12 04:24:24 +00:00
|
|
|
super().__init__()
|
2020-06-15 04:20:55 +00:00
|
|
|
self.campaign_pk = campaign_pk
|
|
|
|
self.contact_pks = contact_pks
|
2020-06-12 04:24:24 +00:00
|
|
|
self.messages = messages
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
try:
|
|
|
|
logger.info(f"Sending {len(self.messages)} emails")
|
|
|
|
send_mass_html_mail(self.messages)
|
2020-06-15 05:47:52 +00:00
|
|
|
logger.info("Emails finished sending")
|
2020-06-15 04:20:55 +00:00
|
|
|
with transaction.atomic():
|
2020-06-15 05:04:48 +00:00
|
|
|
Campaign.objects.filter(pk=self.campaign_pk).update(
|
2020-06-15 04:20:55 +00:00
|
|
|
status=CampaignStatus.SENT,
|
|
|
|
sent_date=timezone.now(),
|
2020-06-15 05:04:48 +00:00
|
|
|
)
|
2020-06-15 05:47:52 +00:00
|
|
|
fresh_contacts = Contact.objects.filter(
|
|
|
|
pk__in=self.contact_pks)
|
|
|
|
Campaign.objects.get(
|
|
|
|
pk=self.campaign_pk).receipts.add(*fresh_contacts)
|
|
|
|
except SMTPException:
|
2020-06-15 05:04:48 +00:00
|
|
|
logger.exception(f"Problem sending campaign: {self.campaign_pk}")
|
2020-06-12 04:24:24 +00:00
|
|
|
self.campaign.status = CampaignStatus.FAILED
|
2020-06-15 05:04:48 +00:00
|
|
|
finally:
|
|
|
|
close_old_connections()
|
2020-06-12 04:24:24 +00:00
|
|
|
|
2020-04-30 07:43:10 +00:00
|
|
|
|
|
|
|
class SMTPEmailBackend(BaseEmailBackend):
|
2020-08-18 00:46:25 +00:00
|
|
|
def send_campaign(self, request, campaign, contacts, test_send=False):
|
2020-04-30 07:43:10 +00:00
|
|
|
messages = []
|
|
|
|
|
|
|
|
for contact in contacts:
|
|
|
|
content = render_to_string(
|
|
|
|
campaign.get_template(request),
|
|
|
|
campaign.get_context(request, contact),
|
|
|
|
)
|
2020-05-19 01:29:14 +00:00
|
|
|
messages.append({
|
2020-06-12 04:24:24 +00:00
|
|
|
'subject': campaign.subject,
|
2020-05-19 01:29:14 +00:00
|
|
|
'body': content,
|
|
|
|
'from_email': self.from_email,
|
|
|
|
'to': [contact.email],
|
|
|
|
'reply_to': [self.reply_to],
|
|
|
|
})
|
2020-08-18 00:46:25 +00:00
|
|
|
if test_send:
|
|
|
|
# Don't mark as complete, don't worry about threading
|
|
|
|
send_mass_html_mail(messages)
|
|
|
|
else:
|
|
|
|
campaign_thread = SendCampaignThread(
|
|
|
|
campaign.pk, [c.pk for c in contacts], messages)
|
|
|
|
campaign_thread.start()
|