Django:邮件未通过视图发送

时间:2018-07-17 06:44:22

标签: python django email gmail

我试图通过django视图发送我的邮件,但是它正在控制台中打印,并且我的邮箱中没有收到任何邮件,但是当我尝试通过python shell进行相同的设置时,我收到了邮件。

Settings.py:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'mygmailusername@gmail.com'
EMAIL_HOST_PASSWORD = 'mygmailpassword'
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL = 'mygmailusername@gmail.com'

views.py:

def login_view(request):
    #print (request.user.is_authenticated())
    title="Sign in"
    form=UserLoginForm(request.POST or None)
    if form.is_valid():
        username=form.cleaned_data.get('username')
        password=form.cleaned_data.get('password')
        user=authenticate(username=username, password=password)
        subject='Test registration'
        message='New user registered.\n Welcome to DD.'
        from_email=settings.EMAIL_HOST_USER
        to_list=[user.email, settings.EMAIL_HOST_USER]
        send_mail(subject,message,from_email,to_list,fail_silently=True)
        login(request,user)
       # print (request.user.is_authenticated())
        return redirect("/index")

    return render(request, "form.html", {"form": form, "title" : title})

出于测试目的,我将其保留在登录视图中。

谢谢!

0 个答案:

没有答案