谷歌SMTP电子邮件发送不起作用的Python

时间:2018-09-02 13:57:06

标签: python django smtp

我正在尝试学习如何使用Google smtp发送电子邮件。我只是想验证我是否可以发送电子邮件。我希望它们起作用的方式是,当用户注册到我的网站时,它将自动向他们发送一封电子邮件,告诉他们“感谢注册...”

settings.py

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

views.py(仅显示注册)

from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.contrib.auth import logout, authenticate, login
from django.contrib.auth.forms import UserCreationForm
from .forms import RegisterForm
from django.core.mail import EmailMessage, send_mail
from django.conf import settings

def register(request):
    """" Register a new user. """
    if request.method != 'POST':
        # Display blank registration form
        form = RegisterForm()
    else:
        # Process completed form.
        form = RegisterForm(data=request.POST)

        new_user = form.save()
        # Log the user in then redirect to home page.
        authenticated_user = authenticate(
            username=new_user.username, password=request.POST['password1'])
        login(request, authenticated_user)
        return HttpResponseRedirect(reverse('blogging_logs:index'))

        subject = "Thank you for registering to blogging site."
        from_email = settings.EMAIL_HOST_USER
        to_email = ['sending_to@gmail.com']
        body = "Thanks for registering to the website let me know what you think"
        send_mail(subject=subject, from_email=from_email,
                  recipient_list=to_email, message=body, fail_silently=False)

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

创建新用户时没有错误。但我没有收到我要发送的电子邮件。

0 个答案:

没有答案