更新用户后如何发送电子邮件?

时间:2018-12-06 17:01:29

标签: python django updates sendmail

我想要在更新用户时,他/她收到一封电子邮件,通知用户已发生更新,我尝试了类似的操作:

 @transaction.atomic
def update_student(request):

        try:
            student = request.user.student

        except Student.DoesNotExist:

            student = Student(user=request.user)

        if request.method == 'POST':
            form = StudentUpdateForm(request.POST, instance=student)
            if form.is_valid():
                form.save
                subject = 'Usuario atualizado'
                from_email = settings.EMAIL_HOST_USER
                to_email = [student.email]
                update_message = 'Usuario ativado com sucesso'
                send_mail(subject,from_email=from_email,recipient_list=to_email,message=update_message,fail_silently=False)
                return redirect('student_list')
            else:
                form = StudentUpdateForm(instance=student)
                return render('student_list')

但是没有成功:((电子邮件没有发送)

有什么建议吗?

电子邮件设置/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 ='youemail@gmail.com' 
EMAIL_HOST_PASSWORD = 'youpassword'
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

在这种情况下,无法发送电子邮件,但是要重置密码是

1 个答案:

答案 0 :(得分:0)

检查您的settings.py文件:

#GMAIL
EMAIL_HOST = 'smtp.gmail.com' # for gmail
EMAIL_HOST_USER = 'xxxxxx@gmail.com' #your email
EMAIL_HOST_PASSWORD = '*********' #your pass
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'xxxxxx@gmail.com' #your email

在Google中控制对不安全应用程序的访问。