如何将电子邮件发送到用户的电子邮件? (帮助Django)

时间:2018-12-07 23:55:34

标签: python django email sendmail

如何将电子邮件发送到用户的电子邮件? 嗨,我想使用他的电子邮件向用户发送电子邮件,通知该用户已更新,我尝试了以下形式的

forms.py

@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')

没问题,但是电子邮件没有发送,因此我在students / views.py中使用了send_email,并且电子邮件已正确发送,但是在“从”和“至”字段中使用“静态”电子邮件< / p>

学生/views.py

class StudentUpdate(UpdateView):
    model = Usuario
    form_class = StudentUpdateForm
    context_object_name = 'student_update'
    template_name = 'students/student_update.html'

    success_url = reverse_lazy('student_list')
    send_mail(
        'Subject here',
        'Here is the message.',
        'from@example.com',
        ['to@example.com'],
        fail_silently=False,
    )

电子邮件设置/设置.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

如何使用用户的电子邮件,而不是使用字符串声明“静态”电子邮件?

0 个答案:

没有答案