-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
22 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |