为什么我的django代码没有将电子邮件发送给我的新注册用户?

我想通过向用户的电子邮件发送确认令牌来验证新用户注册。该代码在控制台中未显示任何错误,但也没有将电子邮件发送给用户电子邮件。我正在使用django 3.0和python 3.8。

the code is executing this part, when i signup a new user, this message poput but i am not receiving email "Open your email to activate account."

帮我解决这个问题:

views.py

from .forms import CreateUserForm
from django.views import View

from django.utils.encoding import force_bytes, force_text
from django.utils.http import urlsafe_base64_decode, urlsafe_base64_encode
from django.contrib.sites.shortcuts import get_current_site
from django.template.loader import render_to_string

from django.core.mail import EmailMessage
from .tokens import activation_token

from django.contrib.auth.models import User

from django.contrib import messages


class RegisterPageView(View):
    def get(self, request, *args, **kwargs):
        form = CreateUserForm()
        template_name = "register/register.html"
        return render(request, template_name, {"form": form})

    def post(self, request, *args, **kwargs):
        form = CreateUserForm(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            user.is_active = False
            user.save()
            message_subject = "Activate your account"
            domain_url = get_current_site(request)
            user_email = form.cleaned_data["email"]
            message = render_to_string(
                "Backend/accounts/activation_message.html",
                {
                    "domain": domain_url.domain,
                    "user": user,
                    "uid": urlsafe_base64_encode(force_bytes(user.id)),
                    "token": activation_token.make_token(user),
                },
            )
            email = EmailMessage(message_subject, message, to=[user_email])
            email.send()
            activation_msg = "Open your email to activate account."
            return render(
                request, "Backend/accounts/activate_email.html", {"activation_msg": activation_msg}
            )

        template_name = 'register/register.html'
        return render(request, template_name, {"form": form})

urls.py

from django.urls import path
from .views import *
from django.contrib.auth import views as auth_views

urlpatterns = [
    path('', index, name="index"),
    path("activate/<uidb64>/<token>", activate, name="activate"),
    path("register/", RegisterPageView.as_view(), name="register"),
]

表格

from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django import forms


class CreateUserForm(UserCreationForm):
    class Meta:
        model = User
        fields = ("username", "email", "password1", "password2","first_name","last_name")

    def clean_email(self):
        data = self.cleaned_data["email"]
        try:
            user_email = User.objects.get(email=data)
        except User.DoesNotExist:
            pass
        else:
            raise forms.ValidationError("Email already exist")

settings.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'mymail'
EMAIL_HOST_PASSWORD = 'mypassword'
评论
客官,不要
客官,不要

您在setting.py中的电子邮件设置应如下所示:

EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.gmail.com"  # mail service smtp
EMAIL_HOST_USER = "youremail@gmail.com"  # mymial is not any email: use environment variable to hide your email in production.
EMAIL_HOST_PASSWORD = "your email password"  # password
EMAIL_PORT = 587
EMAIL_USE_TLS = True

注意:您应该使Gmail帐户的安全性降低。

To to this link

And do as in figure. enter image description here

点赞
评论