django发送邮件通知

1、参考url:

https://www.cnblogs.com/zyj-python/p/7522471.html

注意:

如设置EMAIL_USE_SSL = True #是否使用SSL加密,qq企业邮箱要求使用,

端口需要修改为: 465

具体端口修改参考:https://blog.csdn.net/areigninhell/article/details/79917388

2、views代码样例:

多线程方式实现:

2.1,调用

sqlOrderSendEmal2Create(sqlOrder.id, self.request.get_host).start()

2.2、方法代码

import threading
from django.template import loader
from django.core import mail
from django.conf import settings
class sqlOrderSendEmal2Create(threading.Thread):
    '''
    创建订单邮件
    '''
    def __init__(self,id,host):
        super().__init__()
        self.sqlOrderId = id
        self.host = host
    def run(self):
        self.sendMail()
    def sendMail(self):
        sqlOrder = SqlOrder.objects.get(id=self.sqlOrderId)
        render_args = {'obj': sqlOrder, 'server_host': self.host,
                       'assigned_user': sqlOrder.assigned,}
        to_applyer_content = loader.render_to_string(
            'sqlapply/sqlOrder_create_notice_to_assigned.html', render_args)
        connection = mail.get_connection()
        connection.open()
        messages = []
        # 通知审核人
        if Users.objects.get(username=sqlOrder.assigned).email:
            assigned_mails = [Users.objects.get(username=sqlOrder.assigned).email, ]
            to_assigned_subject = 'SQL执行申请 - 申请通知 - {0} - 单号:{1}'.format(sqlOrder.text, sqlOrder.work_id)
            to_assigned_msg = mail.EmailMessage(to_assigned_subject, to_applyer_content, settings.EMAIL_HOST_USER,
                                                assigned_mails)
            to_assigned_msg.content_subtype = 'html'
            messages.append(to_assigned_msg)
        connection.send_messages(messages)
        connection.close()

2.3、邮件内容模板




    
    运维平台 - SQL执行审核通知  单号{{ obj.work_id }}


运维平台 - SQL执行审核通知 单号{{ obj.work_id }}

{{ obj.username }},您好:

您发起的单号为:{{ obj.work_id }} 的 SQL执行申请,已有新进展,请登入运维平台查看。 查看此申请


{% if obj.assigned %} {% endif %} {% ifequal obj.status 0 %} {% endifequal %} {% if obj.text %} {% endif %}
状态: {{ obj.get_status_display }}
链接: http://{{ server_host }}{% url 'sqlapply:mySqlOrder' %}
审批人: {{ obj.assigned }}
驳回说明: {{ obj.rejected }}
工单说明: {{ obj.text }}

 

 

 

 

 

 

 

 

你可能感兴趣的:(django)