Use multiple threads to send emails in Django

Source: Internet
Author: User
Tags email account
1. Added Email settings for settings. py.# MailEMAIL_HOST = 'smtp .gmail.com '# email smtp server EMAIL_PORT = '25' # port EMAIL_HOST_USER = 'Code *** @ gmail.com' # email account EMAIL_HOST_PASSWORD = '****** * ** '# password: EMAIL_USE_TLS = False 2. send an email with views. py
From django. core. mail import EmailMultiAlternativesfrom django. template import loaderfrom settings import EMAIL_HOST_USERimport threadingfrom_email = EMAIL_HOST_USERclass EmailThread (threading. thread): def _ init _ (self, subject, body, from_email, recipient_list, fail_silently, html): self. subject = subjectself. body = bodyself. recipient_list = recipient_listself.from_email = from_emailself.fail_silently = fail_silentlyself.html = htmlthreading. 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. 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 'mail topic '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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.