41π
β
it is ok now ;
import threading
from threading import Thread
class EmailThread(threading.Thread):
def __init__(self, subject, html_content, recipient_list):
self.subject = subject
self.recipient_list = recipient_list
self.html_content = html_content
threading.Thread.__init__(self)
def run (self):
msg = EmailMessage(self.subject, self.html_content, EMAIL_HOST_USER, self.recipient_list)
msg.content_subtype = "html"
msg.send()
def send_html_mail(subject, html_content, recipient_list):
EmailThread(subject, html_content, recipient_list).start()
π€zjm1126
12π
In the long run, it may prove to be a good decision to use a third-party Django application, such as django-mailer, to handle all sorts of asynchronous email sending/management requirements.
π€ayaz
- [Django]-How do I import the Django DoesNotExist exception?
- [Django]-How can I serialize a queryset from an unrelated model as a nested serializer?
- [Django]-Django order_by() filter with distinct()
3π
After checking out more complex solutions based around celery, etc. I found django-post_office (https://github.com/ui/django-post_office) Itβs a very simple database + cron job plugin that took 5 mins to get up and running. Works perfectly on both my local dev machine and Heroku.
π€user2916527
- [Django]-UnicodeDecodeError : 'ascii' codec can't decode byte 0xe0 in position 0: ordinal not in range(128)
- [Django]-ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured
- [Django]-Django Admin: Using a custom widget for only one model field
2π
create new file named send_mail.py
and add
the function to send the mail
def send_html_mail (*args,**kwargs):
subject = kwargs.get("subject")
html_content = kwargs.get("html_content")
recipient_list = kwargs.get("recipient_list")
msg = EmailMultiAlternatives(subject, html_content, EMAIL_HOST_USER, recipient_list)
msg.attach_alternative(True, "text/html")
msg.send()
call a this function in the views.py
import threading
from send_mail import send_html_mail
def my_view (request):
# .....
threading.Thread (
# call to send_html_mail
target=send_html_mail,
kwargs={
"subject":"My super subject",
"html_content":"My super html content",
"recipient_list":["to@mail.com"]
}).start()
# .....
π€Raul Cobiellas
- [Django]-Django dynamic model fields
- [Django]-Multiple images per Model
- [Django]-What does request.method == "POST" mean in Django?
Source:stackexchange.com