当用户将新数据提交到我们的网站时,我正在尝试向管理员发送电子邮件。我们使用Django作为后端,使用Vue作为前端,尽管这可能并不重要。这是代码:
@receiver(post_save)
def send_update(sender, created, **kwargs):
if created:
data=kwargs['instance']
try:
if data.admin_approved == False:
print("point 1 reached")
name = data.submitted_by_name
body = data.body
content_type = str(sender).split(".")[2][:-2]
print("point 2 reached")
link = "https://link_to_website.com" + content_type.lower()
subject = "New " + content_type + " submitted"
print("point 3 reached")
from_email = "NoReply@web_site.com"
to_email = "my_email@address.com"
print("pre-html point reached")
html_message = get_template('./email/template.html')
text_message = get_template('./email/textplate.txt')
data = {
'user_name': name,
'submission': data.body,
'type': content_type,
'link': link,
'body': body
}
content_text = text_message.render(data)
content_html = html_message.render(data)
print("ready to send email!")
msg = EmailMultiAlternatives(subject, content_text, from_email, [to_email])
msg.attach_alternative(content_html, "text/html")
msg.send()
except:
print("Data was not submitted by an non-admin user.")
包含try / except,以便直接通过django管理页面提交的数据不会触发电子邮件功能。
该功能工作到“到达pre-html点”为止,我猜问题出在msg和msg.send()内,但是我没有收到任何错误功能。
感谢您的帮助!