在Django中調用exchange發送HTML郵件

爲了以後使用方便,記錄一下。

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

wKioL1PjFsaR4oahAABq8HwylXE347.jpg

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 = '[email protected]'
        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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章