为什么我的django电子邮件设置不发送电子邮件通知?

时间:2019-02-17 08:02:08

标签: python-3.x django-views django-email

我创建了一个注册页面,该页面会在成功注册后发送电子邮件通知。我确实有我的电子邮件通知。但是它似乎没有用,也没有抛出任何错误。 下面是代码段。

views.py

def register(request):
    validators = [MinimumLengthValidator, NumberValidator, UppercaseValidator]
    if request.method == 'POST':
        first_name = request.POST['first_name']
        last_name = request.POST['last_name']
        email = request.POST['email']
        username = request.POST['username']
        password = request.POST['password']
        try:
            for validator in validators:
                validator().validate(password)
        except ValueError as e:
            messages.error(request, str(e))
            return redirect('register')
        password2 = request.POST['password2']

        # check if the password match
        if password == password2:

            if User.objects.filter(username=username).exists():
                messages.error(request, 'username already exist')
                return redirect('register')
            else:
                if User.objects.filter(email=email).exists():
                    messages.error(request, 'Registration Failed - Try different email address')
                    return redirect('register')
                else:
                    user = User.objects.create_user(username=username, password=password, email=email,
                                                    first_name=first_name, last_name=last_name)
                    user.save()
                    messages.success(request, 'Registration complete, please proceed to login')
                    return redirect('register')
        else:
            messages.error(request, 'password dose not match')
            return redirect('register')
    else:
        return render(request, 'ACCOUNTS/register.html')


def ThankYou(request, register):
    if request.method == 'POST':
        if register.is_valid():
                save_it = register.save(commit=False)
                save_it.save()
                subject = 'Registration successful'
                message = 'Thank you for registration, please continue with the login'
                from_email = settings.EMAIL_HOST_USER
                to_list = [save_it.email, settings.EMAIL_HOST_USER]
                try:
                    send_mail(
                        subject,
                        message,
                        from_email,
                        [to_list],
                        fail_silently=False,
                    )
                except ValueError:
                    return HttpResponse('Invalid header found.')
        else:
            messages.success(request, 'thank you ')
            return redirect('register')
    else:
        return redirect('index')

下面是我的设置。py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = False
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'ami.sahay@gmail.com'
EMAIL_HOST_PASSWORD = '123'
DEFAULT_FROM_EMAIL = 'ami.sahay@gmail.com'

我已经阅读了几个Stackoverflow帖子,并根据建议进行了一些更改。但是它们似乎没有任何作用。以下是链接之一。

Django Doesn't Send Email Notifications

0 个答案:

没有答案