我正在尝试在执行某个查询时发送邮件。但我在连接中遇到错误。我在settings.py文件中尝试了以下设置
server = smtplib.SMTP('smtp.gmail.com')
server.starttls()
EMAIL_HOST_USER = "email@gmail.com"
EMAIL_HOST_PASSWORD = "password"
server.login(EMAIL_HOST_USER, EMAIL_HOST_PASSWORD)
我已执行以下命令发送电子邮件:
emailFrom = [settings.EMAIL_HOST_USER]
html_content = render_to_string('template.html', {'greeting_text': greeting_text})
text_content = strip_tags(html_content)
email_subject = "Subject"
msg = EmailMultiAlternatives(email_subject,text_content,emailFrom,[email],)
msg.attach_alternative(html_content, "text/html")
msg.send()
但每当我运行上面的代码时,我得到'请首先运行connect()'错误。 什么是错误究竟是什么以及如何解决这个问题?
答案 0 :(得分:5)
确保您已配置所有电子邮件设置:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True