djangodj-rest-auth

dj rest auth reset password sends raw html in email


I use dj rest auth package and i have overrided the reset password email like this

in settings.py :

REST_AUTH_SERIALIZERS = {
'JWT_TOKEN_CLAIMS_SERIALIZER': 'account.token.TokenObtainPairSerializer',
'USER_DETAILS_SERIALIZER': 'account_profile.api.userupdate.UserSerializer',
'PASSWORD_RESET_SERIALIZER': 'account_profile.api.customemail.CustomPasswordResetSerializer'}

and in customemail.py :

class CustomPasswordResetSerializer(PasswordResetSerializer):
def save(self):
    request = self.context.get('request')
    # Set some values to trigger the send_email method.
    opts = {
        'use_https': request.is_secure(),
        #'from_email': 'noreply@mizbans.com',
        'from_email': getattr(settings, 'DEFAULT_FROM_EMAIL'),
        'request': request,
        
        # here I have set my desired template to be used
        # don't forget to add your templates directory in settings to be found
        'email_template_name': 'password_reset_email.html'
    }

    opts.update(self.get_email_options())
    self.reset_form.save(**opts)

and in email.py :

class MyPasswordResetSerializer(PasswordResetSerializer):
context={'domain':settings.WEBSITE_DOMAIN,'site_name':'mizban'}

def get_email_options(self) :
  
    return {
        'email_template_name': 'password_reset_email.html',
        'context':'context'
    }

I put a sample html code in password_reset_email :

 <html>
    <body>
      <p>welcome to{{domain}}</p>
      <p>site {{site_name}}</p>
    </body>
</html>

It works but it sends html tages codes as well in email template!


Solution

  • I solved the bug by adding this :

    'html_email_template_name': 'password_reset_email.html'
    

    to opts={} in save method