How to use a different domain for Djoser email?
Asked Answered
P

3

7

How can I change the domain the link inside the email djoser sends uses?

Petterson answered 26/6, 2020 at 1:50 Comment(0)
P
9

I figured this out, to change the link domain you need to add DOMAIN and SITE_NAME to your project settings. Example:

DOMAIN = config('DOMAIN') #example.com
SITE_NAME = config('SITE_NAME') #Example

 DJOSER = {
    'LOGIN_FIELD':'email',
    'USER_CREATE_PASSWORD_RETYPE':True,
    'ACTIVATION_URL': '/users/activate/{uid}/{token}',
    'SEND_ACTIVATION_EMAIL': True,
    'SERIALIZERS':{
        'user_create':'userauth.serializers.UserCreateSerializer',
        'user':'userauth.serializers.UserCreateSerializer',
        'activation': 'djoser.email.ActivationEmail',
}

Then you should get the next link in your email:

http://example.com/auth/users/activate/MQ/5c9-26bcab9e85e8a967731d
Petterson answered 26/6, 2020 at 1:50 Comment(3)
Hello, did you succeed to get https://<your_domain> ? By default it is always httpDemetria
@Demetria did you find out how to get https instead of http?Battista
@Battista . According to source code of the package that Djoser used, protocol will automatically convert to https if you use https for your backend.Cataphyll
G
0

{{ protocol }} variable in email templates rised from templated_mail library,

protocol = context.get('protocol') or (
    'https' if self.request.is_secure() else 'http'
)

so, with https requests on production server protocol will be https

Gotha answered 27/6, 2023 at 7:49 Comment(0)
D
0

as mentioned, for the "domain" and "site_name", it can be defined in settings.py:

DOMAIN = "example.com"
SITE_NAME = "Example"

but for the protocol there is two approaches:

1- I didn't test it, but it should works,

in a production environment if you add in your setting: SECURE_SSL_REDIRECT=True, it will change the default protocol in emails from "http" to "https"

2- anyway, you can override the ActivationEmail (or other email functions) to force them to change the protocol from "http" to "https"

class ActivationEmail(BaseEmailMessage):
    template_name = 'email/activation.html'

    def get_context_data(self):
        # ActivationEmail can be deleted
        context = super().get_context_data()

        user = context.get("user")
        context["uid"] = utils.encode_uid(user.pk)
        context["token"] = default_token_generator.make_token(user)
        context["url"] = settings.ACTIVATION_URL.format(**context)
        context["protocol"] = "https"
        return context
Downward answered 21/8, 2023 at 8:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.