首页 > 解决方案 > 错误:使用 django 电子邮件我在发送电子邮件后发送电子邮件出错?smtplib.SMTPServerDisconnected:[WinError 10054]

问题描述

我是初学者,使用 django 文档发送电子邮件。

提交表单时遇到问题。我收到了这个错误:

SMTPServerDisconnected at /buggy_app/booking/

在我的终端中,我得到了:

smtplib.SMTPServerDisconnected: Connection unexpectedly closed: [WinError 10054]

视图.py

from django.template.loader import get_template
from django.conf import settings
from django.core.mail import EmailMultiAlternatives

class BookingView(FormView):
    template_name = 'buggy_app/booking.html'
    form_class = BookingForm
    models = Booking

    def form_valid(self, form):
        car_id = self.request.GET.get('car', '')
        car = Car.objects.get(id=car_id)
        car.is_available_car = False
        car.save()
        form.save()
        form.cleaned_data.get('username')
        first_name = form.cleaned_data.get('first_name')
        last_name = form.cleaned_data.get('last_name')
        to_email = form.cleaned_data.get('email')

        send_emails(first_name, last_name, to_email)

        return super(BookingView, self).form_valid(form)

    def send_emails(first_name, last_name, to_email):
        htmly = get_template('buggy_app/welcome.html')
        d = {'first_name':first_name, 'last_name':last_name}
        subject, from_email, to = 'Subject line', settings.EMAIL_HOST_USER, to_email

        html_content = htmly.render(d)
        msg = EmailMultiAlternatives(subject, html_content, from_email, [to])
        msg.attach_alternative(html_content, "text/html")
        msg.send()

标签: djangodjango-viewsdjango-email

解决方案


推荐阅读