Django发HTML邮件

1.settings配置

1 EMAIL_HOST = 'XXXX'
2 DEFAULT_FROM_EMAIL = '张宁 <zhang.ning@XXX.com>'
3 RECEIVER =['zhang.ning@XXX.com']
4 CCER=['zhang.ning@XXX.com',]

2.views

 1 from django.shortcuts import render, HttpResponse
 2 from django.template import loader#*****
 3 from django.core.mail import EmailMultiAlternatives #***
 4 from django.conf import settings#****
 5 #只要定义好主题和内容即可,其他的在配置文件使用
 6 # Create your views here.
 7 def index(request):
 8     try:
 9         subject = 'hello'
10         html_content = loader.render_to_string('tb.html')
11         msg = EmailMultiAlternatives(subject, html_content, settings.DEFAULT_FROM_EMAIL, settings.RECEIVER,settings.CCER)
12         msg.content_subtype = "html"
13         msg.send()
14     except Exception as e:
15         return HttpResponse("NO")
16     return HttpResponse("ok")

 

posted @ 2018-12-20 15:53  zhang.ning  阅读(258)  评论(0编辑  收藏  举报