Skip to content

Commit

Permalink
feat: add function to send welcome email to previous subscribers (#601)
Browse files Browse the repository at this point in the history
  • Loading branch information
vncsna authored May 17, 2024
1 parent 0ee27b6 commit de6fdcc
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion bd_api/apps/account/signals.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
from datetime import datetime

from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from django.db.models.signals import post_save
Expand All @@ -7,7 +9,7 @@
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode

from bd_api.apps.account.models import Account
from bd_api.apps.account.models import Account, Subscription
from bd_api.apps.account.token import token_generator
from bd_api.custom.environment import get_frontend_url

Expand Down Expand Up @@ -47,3 +49,37 @@ def send_activation_email_signal(sender, instance, created, raw, **kwargs):
"""
if created and not raw and not instance.is_active:
send_activation_email(instance)


def send_welcome_email(account: Account):
"""Send welcome email to account"""
to_email = account.email
from_email = settings.EMAIL_HOST_USER
subject = "Bem Vindo à Base dos Dados!"

token = token_generator.make_token(account)
uid = urlsafe_base64_encode(force_bytes(account.pk))

content = render_to_string(
"account/welcome_email.html",
{
"name": account.get_full_name(),
"domain": get_frontend_url(),
"uid": uid,
"token": token,
},
)

msg = EmailMultiAlternatives(subject, "", from_email, [to_email])
msg.attach_alternative(content, "text/html")
msg.send()


def send_welcome_email_to_previous_subscribers():
"""Send welcome email to all previous subscribers"""

for subscription in Subscription.objects.filter(
subscription__status="active",
subscription__created__lte=datetime(2023, 12, 1),
).all():
send_welcome_email(subscription.admin)

0 comments on commit de6fdcc

Please sign in to comment.