Home > Backend Development > Python Tutorial > How to call exchange to send HTML emails in Django

How to call exchange to send HTML emails in Django

高洛峰
Release: 2017-03-28 11:09:24
Original
2198 people have browsed it

For future convenience, record it.

Add the content "EMAIL_USE_TLS = True" in the first line of project settings.py

How to call exchange to send HTML emails in Django

2. Script content

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
Copy after login

The above is the detailed content of How to call exchange to send HTML emails in Django. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template