renator's picture
Updated
99122b8
from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.core.mail import send_mail
from django.conf import settings
from django.contrib.auth import get_user_model
from django.utils.encoding import force_bytes
from django.utils.html import strip_tags
from django.utils.http import urlsafe_base64_encode
def send_email_to_user(
user_pk,
host,
email,
email_type,
new_email=None,
from_email=settings.EMAIL_HOST_USER,
**kwargs,
):
user = get_user_model().objects.get(pk=user_pk)
token_generator = PasswordResetTokenGenerator()
token = token_generator.make_token(user)
uid = urlsafe_base64_encode(force_bytes(user.pk))
reset_link = f"{host}/verify/{uid}/{token}"
# Define your email subject
subject = "Reset Your Password for Text to Speech AI Voice Generator"
# Write the HTML content directly in the function
html_content = f"""
<html>
<body>
<p>Hello {user.get_full_name() or user.username},</p>
<p>You requested a password reset for your account. Click the link below to reset your password:</p>
<p><a href="{reset_link}">Reset Password</a></p>
<p>If you didn't request this, you can ignore this email.</p>
<p>Your link will expire in 48 hours.</p>
<p>Thank you!</p>
</body>
</html>
"""
# Create a text-only version of the email
text_content = strip_tags(html_content)
try:
send_mail(
subject,
text_content,
from_email,
[email],
html_message=html_content, # This is the key part for HTML
fail_silently=False,
)
except Exception as e:
print(e)