gpt4 book ai didi

python - 将 send_mail() 方法更改为 EmailMessage - Django 1.6

转载 作者:太空宇宙 更新时间:2023-11-03 17:30:37 26 4
gpt4 key购买 nike

我正在尝试将 Django 1.6 中的 send_mail() 方法更改为 EmailMessage 方法。

这就是我的方法使用 send_mail()

的样子
 # Send email
if user_profile.contracting:
subject = _('Your question on project {} has been answered')
body = _('You can read your answer here {}')
email = question.user.email

else:
subject = _('Your have a new question on project {}')
body = _('You can read your question here: {}')
email = project.user.email

send_mail(subject.format(project.name),
body.format(
os.environ.get('CURRENT_URL') + '/' +
reverse('projects_id', args=(project.id,))[1:]),
'no-responder@contratlos.com', [email])

return HttpResponseRedirect(reverse('projects_id',
args=(project.id,)))

现在,尝试更改它,它不会抛出任何错误,但不会发送任何电子邮件:

 # Send email
if user_profile.contracting:
subject = _('Your question on project {} has been answered')
body = _('You can read your answer here {}')
email = question.user.email

else:
subject = _('Your have a new question on project {}')
body = _('You can read your question here: {}')
email = project.user.email

correo = EmailMessage(subject.format(project.name),
body.format(
os.environ.get('CURRENT_URL') + '/' +
reverse('projects_id', args=(project.id,))[1:]),
'no-responder@contratlos.com', [email],['kristian.koci@gmail.com'])

return HttpResponseRedirect(reverse('projects_id',
args=(project.id,)))

我在这里缺少什么吗?

提前致谢!

最佳答案

EmailMessage 是一个用于创建自定义电子邮件的便捷类。但是,与 send_mail() 不同,您需要显式调用 .send()为了实际发送消息:

correo.send()

关于python - 将 send_mail() 方法更改为 EmailMessage - Django 1.6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31899923/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com