0

I am fairly new to Django and I use Django 4.2. I am trying to send email as rendered templates from views.py. The email sends but in all plain text. The link does not show in the email as a link, it shows as a plain html text.

views.py send email function

def sendActivationMail(user, request):
    current_site = get_current_site(request)
    email_subject = 'Verify your account'
    # context = {
    #     'click_action': 'showDomainLink',
    # }
    email_body = render_to_string(
        'mail_temp/confirm_temp.html',
        {
        'user': user,
        'domain': current_site.domain,
        'uid': urlsafe_base64_encode(force_bytes(user.pk)),
        'token': generator_token.make_token(user),
        }, 
        # context 
    )

    email = EmailMessage(
        subject=email_subject,
        body=email_body,
        from_email=settings.EMAIL_HOST_USER,
        to=[user.email]
    )
    email.content_subtype = 'html'
    email.send()




def activate_token(request, uidb64, token):
    try:
        uid = force_str(urlsafe_base64_decode(uidb64))
        user = Users.objects.get(pk=uid)
    except (ValueError, Users.DoesNotExist):
        user = None
    
    if user is not None and generator_token.check_token(user, token):
        user.is_email_verified = True
        user.save()
        messages.success(request, 'Successfully verified email')
        return redirect(reverse('login'))
    else:
        messages.error(request, 'Verification link was invalid')
        return redirect('index')

email body template


{% autoescape off %} 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

    <style>
        h1{
            color: chocolate;
        }
        p{
            font-size: 22px;
        }
    </style>
</head>
<body>
    <h1>Hey there, you are almost there!</h1>
    <p>Please click link to verify your account</p>
    <a class="underline" href="http://{{ domain }} {% url 'activate' uidb64=uid token=token%}">token</a> 
</body>
</html>

{% endautoescape %}

I have tried to google this problem but I have not found a matching solution. The email keeps sending as plain strings. What do I have to do to get my anchor tag to be visible as a link in the email body.

theocode
  • 69
  • 8

1 Answers1

0

I have an update. It is a silly thing really, but the space between the domain and the url in the template page should not be there.

It should go from this:

 <a class="underline" href="http://{{ domain }} {% url 'activate' uidb64=uid token=token%}">token</a>   

to this:

 <a class="underline" href="http://{{ domain }}{% url 'activate' uidb64=uid token=token%}">token</a> 
theocode
  • 69
  • 8