diff options
author | lxf <sng@luxagraf.net> | 2022-05-14 16:38:07 -0400 |
---|---|---|
committer | lxf <sng@luxagraf.net> | 2022-05-14 16:38:07 -0400 |
commit | bb3973ffb714c932e9ec6dd6a751228dc71fe1d3 (patch) | |
tree | 6fa32f9392ad2ec32271349b86a4c1388fd6ba95 /app/lttr/mailer.py |
initial commit
Diffstat (limited to 'app/lttr/mailer.py')
-rw-r--r-- | app/lttr/mailer.py | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/app/lttr/mailer.py b/app/lttr/mailer.py new file mode 100644 index 0000000..40f1003 --- /dev/null +++ b/app/lttr/mailer.py @@ -0,0 +1,83 @@ +from time import sleep +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText + +from django.core.mail import EmailMultiAlternatives +from django.template.loader import render_to_string +from django.utils.encoding import smart_str + +from .models import Subscriber, MailingStatus + + + +class SendShit(): + + def __init__(self, newsletter, mailing, verbose=0): + self.verbose = verbose + self.newsletter = newsletter + self.mailing = mailing + all_subscribers = Subscriber.objects.filter(newsletter=self.newsletter,subscribed=True,unsubscribed=False).values('email_field') + already_sent = MailingStatus.objects.filter(newsletter_mailing=self.mailing,status=1).values('subscriber__email_field') + self.subscribers = all_subscribers.difference(already_sent) + + + def send_mailings(self): + mailings = len(self.subscribers) + print("mailing newsletter to %s subscribers"%mailings) + i = 1 + for s in self.subscribers: + subscriber = Subscriber.objects.get(newsletter=self.newsletter,email_field=s['email_field'],subscribed=True,unsubscribed=False) + status = None + if self.verbose == 1: + print("mailing newsletter %s of %s to %s" %(i,mailings,subscriber)) + status, created = MailingStatus.objects.get_or_create( + newsletter_mailing=self.mailing, + subscriber=subscriber, + ) + # New instance, try sending + if created: + email = self.build_message(subscriber) + status.status=1 + if self.verbose==1: + print("successfully sent %s the newsletter mailing %s"%(subscriber, self.mailing)) + status.save() + else: + # not new, check if error and resend or just continue + if status.status == 2: + if self.verbose==1: + print("retrying error") + try: + email = self.build_message(subscriber) + status.status=1 + if self.verbose==1: + print("successfully sent %s the newsletter mailing %s"%(subscriber, self.mailing)) + except: + status.status=2 + if self.verbose == 1: + print("failed to send %s to %s"%(self.mailing, subscriber)) + status.save() + i=i+1 + sleep(2) + + + def build_message(self, subscriber): + """ + Build the email as plain text with a + a multipart alternative for HTML + """ + subject = smart_str("%s: %s — %s" %(self.mailing.newsletter.title, self.mailing.get_issue_str(), self.mailing.title)) + from_email, to = 'Scott Gilbertson <sng@luxagraf.net>', subscriber.get_email() + text_content = render_to_string(self.newsletter.get_template_plain(), {'object': self.mailing, 'subscriber':subscriber}) + html_content = render_to_string(self.newsletter.get_template_html(), {'object': self.mailing, 'subscriber':subscriber}) + #print(html_content) + msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) + msg.attach_alternative(html_content, "text/html") + #msg.Header.Add('List-Unsubscribe', '<https://luxagraf.net%s>' % subscriber.unsubscribe_activate_url) + msg.send() + + ''' + for header, value in self.newsletter.server.custom_headers.items(): + message[header] = value + ''' + return msg + |