File size: 1,752 Bytes
99122b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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)