如何在Django中调用exchange发送HTML邮件

高洛峰
高洛峰 原创
2017-03-28 11:09:24 1654浏览

为了以后使用方便,记录一下。

在工程settings.py首行添加内容"EMAIL_USE_TLS = True"

如何在Django中调用exchange发送HTML邮件

2.脚本内容

script file: usermail.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.core.mail import EmailMultiAlternatives,get_connection
from django.template.loader import render_to_string
from django.conf import  settings  
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
 
def userMail(tousers,obj,html_content):
    conn = get_connection()
    conn.username = 'ops'
    conn.password = '123456'
    conn.host = 'exchange.test.com'
    try:
        conn.open()
        EMAIL_HOST_USER = 'ops@test.com'
        subject, from_email, to = obj, EMAIL_HOST_USER, tousers
        msg = EmailMultiAlternatives(subject, html_content, from_email, to)
        msg.attach_alternative(html_content, "text/html")
        conn.send_messages([msg,])
        conn.close()
    except Exception,e:
        print e

以上就是如何在Django中调用exchange发送HTML邮件的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。