错误:使用django电子邮件发送电子邮件出错后,我正在发送电子邮件吗? smtplib.SMTPServerDisconnected:[WinError 10054]

时间:2018-06-25 14:46:45

标签: django django-views django-email

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

提交表单时遇到问题。我收到此错误:

SMTPServerDisconnected at /buggy_app/booking/

在我的终端机上,我得到了:

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

Views.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()

0 个答案:

没有答案