Skip to content

Commit

Permalink
Update tasks.py
Browse files Browse the repository at this point in the history
  • Loading branch information
andyjud authored Jun 22, 2024
1 parent 46e0c1b commit ba59f71
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions a_messageboard/tasks.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
from celery import shared_task
from django.core.mail import EmailMessage
from django.template.loader import render_to_string
from datetime import datetime
from .models import *

@shared_task(name='email_notification')
def send_email_task(subject, body, emailaddress):
email = EmailMessage(subject, body, to=[emailaddress])
email.send()
return emailaddress


@shared_task(name='monthly_newsletter')
def send_newsletter():
subject = "Your Monthly Newsletter"

subscribers = MessageBoard.objects.get(id=1).subscribers.filter(
profile__newsletter_subscribed=True,
)

for subscriber in subscribers:
body = render_to_string('a_messageboard/newsletter.html', {'name': subscriber.profile.name})
email = EmailMessage( subject, body, to=[subscriber.email] )
email.content_subtype = "html"
email.send()

current_month = datetime.now().strftime('%B')
subscriber_count = subscribers.count()
return f'{current_month} Newsletter to {subscriber_count} subs'

0 comments on commit ba59f71

Please sign in to comment.