如何在djoser中自定义发送的电子邮件?

问题描述 投票:0回答:1

我构建了React&Django项目并使用Djoser进行注册和验证。我想在 Gmail 上自定义发送的电子邮件内容。应该去哪里改呢?

django djoser
1个回答
4
投票

在您的 Django 目录中创建一个应用程序或在现有应用程序中

<my_app>
创建文件:

  • 电子邮件.py

然后在

<my_app>
目录中创建模板文件夹,例如:

  • templates/<my_app>/

在目录

<my_app>
中创建两个 HTML 文件:

  • ActivateEmail.html
  • ConfirmationEmail.html

然后在ActivationEmail.html中添加以下代码:

{% load i18n %}

{% block subject %}
{% blocktrans %}Account activation on {{ site_name }}{% endblocktrans %}
{% endblock subject %}

{% block text_body %}
{% blocktrans %}You're receiving this email because you need to finish activation process on {{ site_name }}.{% endblocktrans %}

{% trans "Please go to the following page to activate account:" %}
{{ protocol }}://{{ domain }}/{{ url|safe }}

{% trans "Thanks for using our site!" %}

{% blocktrans %}The {{ site_name }} team{% endblocktrans %}
{% endblock text_body %}

{% block html_body %}
<p>{% blocktrans %}You're receiving this email because you need to finish activation process on {{ site_name }}.{% endblocktrans %}</p>

<p>{% trans "Please go to the following page to activate account:" %}</p>
<p><a href="{{ protocol }}://{{ domain }}/{{ url|safe }}">{{ protocol }}://{{ domain }}/{{ url|safe }}</a></p>

<p>{% trans "Thanks for using our site!" %}</p>

<p>{% blocktrans %}The {{ site_name }} team{% endblocktrans %}</p>

{% endblock html_body %}

并在您的ConfirmationEmail.html中添加以下代码:

{% load i18n %}

{% block subject %}
{% blocktrans %}{{ site_name }} - Your account has been successfully created and activated!{% endblocktrans %}
{% endblock %}

{% block text_body %}
{% trans "Your account has been created and is ready to use!" %}

{% trans "Thanks for using our site!" %}

{% blocktrans %}The {{ site_name }} team{% endblocktrans %}
{% endblock text_body %}

{% block html_body %}
<p>{% trans "Your account has been created and is ready to use!" %}</p>

<p>{% trans "Thanks for using our site!" %}</p>

<p>{% blocktrans %}The {{ site_name }} team{% endblocktrans %}</p>
{% endblock html_body %}

然后在您的应用程序中的 email.py

<my_app>
添加以下代码:

from djoser import email
from djoser import utils
from djoser.conf import settings
from django.contrib.auth.tokens import default_token_generator


class ActivationEmail(email.ActivationEmail):
    template_name = '<my_app>/ActivationEmail.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)
        return context


class ConfirmationEmail(email.ConfirmationEmail):
    template_name = '<my_app>/ConfirmationEmail.html'

然后编辑内容:

  • 激活电子邮件.html
  • 确认电子邮件.html

随你喜欢。

最后一步是添加

email.py
中的类来替换默认模板 激活电子邮件确认电子邮件。 默认值如文档中所示:djoser docs 然后在
settings.py
文件中的 Djoser 设置中:

DJOSER = {
    'USER_CREATE_PASSWORD_RETYPE': True,
    'SEND_CONFIRMATION_EMAIL': True,
    'SEND_ACTIVATION_EMAIL': True,
    'ACTIVATION_URL': 'activate/{uid}/{token}',
    'HIDE_USERS': True,
    'SERIALIZERS': {
        'user': 'myapp.serializers.UserCreateSerializer',
        'user_create': 'myapp.serializers.UserCreateSerializer',
        'current_user': 'myapp.serializers.UserSerializer',
    },
    (...)

}

添加此:

DJOSER = {
    'USER_CREATE_PASSWORD_RETYPE': True,
    'SEND_CONFIRMATION_EMAIL': True,
    'SEND_ACTIVATION_EMAIL': True,
    'ACTIVATION_URL': 'activate/{uid}/{token}',
    'HIDE_USERS': True,
    'SERIALIZERS': {
        'user': 'myapp.serializers.UserCreateSerializer',
        'user_create': 'myapp.serializers.UserCreateSerializer',
        'current_user': 'myapp.serializers.UserSerializer',
    },
    'EMAIL': {
        'activation': 'myapp.email.ActivationEmail',
        'confirmation': 'myapp.email.ConfirmationEmail',
    },
    (...)
}

然后发送电子邮件激活,而不是在用户的电子邮件中发送默认模板,它将为他们发送 ActivationEmail.html 模板

注意,Djoser 使用 django-template-mail 作为其激活电子邮件模板。

© www.soinside.com 2019 - 2024. All rights reserved.