尝试在django视图中发送邮件
from django.core.mail import send_mail
# Snippet inside a view
for ml in mls:
try:
l = Letter.objects.get(pk = ml.message_key)
except Exception as e:
mail_txt = _("sending mail failed " + str(e) + " " + str(ml.activity_org) + " " + str(ml.scheduled_time))
send_mail(mail_txt, "sending mail", "user@domain.com", ["sender@domain.com"], fail_silently=False)
错误
NameError: global name 'send_mail' is not defined
答案 0 :(得分:0)
我建议您使用EmailMultiAlternatives
。我用这个,很容易配置和使用。
例如:
将此添加到Settings.py
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'yourmail@gmail.com'
EMAIL_HOST_PASSWORD = 'yourpass'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
如果使用其他邮件服务器,请更改数据的显示数据
功能示例
from django.core.mail import EmailMultiAlternatives
title = 'Test Email'
message_text = 'Test email message'
#email_to has to be a list, even if you're sending mail only to 1 address
mail = EmailMultiAlternatives(title, message_text, 'from@email.com', ['email_to1'])
mail.send()
您可以附加文件或HTML代码... Django Documentation - Mail
中的更多信息希望它有所帮助!