django - 错误:使用 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()
解决方案
推荐阅读
- python - 如何映射其他两个匹配的列值?“重新索引仅对具有唯一值的索引对象有效”?
- python - 如何在python中实现结构数据类型
- web - 托管单页静态网站有哪些选择
- serialization - Newtonsoft.Json 序列化/反序列化静态类
- css - CSS Grid 项目溢出身体(使滚动条出现)
- jenkins - 使用 AWSCLI 从 Jenkins 更新 ECS 集群
- php - 在特定条件下计算两天之间的天数
- mysql - 在没有mysqldump的情况下通过nodejs恢复MySQL sql转储
- linux - 如何在没有 SET / SELECTS / -- POSTGRESQL 中的评论的情况下仅对 INSERT 语句进行 pg_dump?
- google-cloud-bigtable - 如何将数据从一个 BigTable 表复制到另一个 BigTable 表