zoukankan      html  css  js  c++  java
  • Django中使用多线程发送邮件

    1、settings.py 增加Email设置
     
    #mail
    EMAIL_HOST = ‘smtp.gmail.com’                   #邮件smtp服务器
    EMAIL_PORT = ’25′                                        #端口
    EMAIL_HOST_USER = ‘code***@gmail.com’  #邮件账户
    EMAIL_HOST_PASSWORD = ‘*********’      #密码
    EMAIL_USE_TLS = False
    2、views.py 发送邮件
    from django.core.mail import EmailMultiAlternatives
    from django.template import loader
    from settings import EMAIL_HOST_USER
    import threading
    from_email = EMAIL_HOST_USER
    class EmailThread(threading.Thread):
      def __init__(self, subject, body, from_email, recipient_list, fail_silently, html):
        self.subject = subject
        self.body = body
        self.recipient_list = recipient_list
        self.from_email = from_email
        self.fail_silently = fail_silently
        self.html = html
        threading.Thread.__init__(self)
      def run (self):
        msg = EmailMultiAlternatives(self.subject, self.body, self.from_email, self.recipient_list)
        if self.html:
          msg.attach_alternative(self.body,self.html)
        msg.send(self.fail_silently)
    def send_mail(subject, body, from_email, recipient_list, fail_silently=False, html=None, *args, **kwargs):
      EmailThread(subject, body, from_email, recipient_list, fail_silently, html).start()
    def test_mail(request):
      subject = u'邮件主题'
      to_mail_list = ['abc@gmail.com','test@qq.com']
      body = loader.render_to_string('mail_template.html',
      {'email':email,'date':datetime.today(), }
      )
      send_mail(subject,body,from_email,to_mail_list,html="text/html")
      return "ok"
     
     
  • 相关阅读:
    jdk环境变量及1.6官方下载地址
    linux创建用户和用户组
    java左移右移运算符
    浏览器是如何存储密码的
    【转】 ip段/数字,如192.168.0.1/24是什么意思?
    动态代理实现AOP【转】
    java自定义注解注解方法、类、属性等等【转】
    oracle插入主键数据、sequence和触发器
    ThreadLocal意为变量副本
    【转】java内部类的作用
  • 原文地址:https://www.cnblogs.com/baoyiluo/p/3644207.html
Copyright © 2011-2022 走看看