Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

Slow e-mail #60

Open
Dean-Christian-Armada opened this issue Jul 21, 2017 · 4 comments
Open

Slow e-mail #60

Dean-Christian-Armada opened this issue Jul 21, 2017 · 4 comments

Comments

@Dean-Christian-Armada
Copy link

The contact-form is slow to react when sending e-mails. Seems like it is synchronous. Is there an available setting to make it asynchronous?

@mishbahr
Copy link
Owner

mishbahr commented Jul 21, 2017

This is something that is not supported out of the box.

You should subclass FormBuilder form and implement your own .email_submission() method and then simply override form_class in FormSubmission view by defining your own URL.

#urls.py
from django.conf.urls import url
from djangocms_forms.views import FormSubmission
from project.forms import AsyncFormBuilder  # <= custom form

urlpatterns = [
    url(r'^forms/submit/$', FormSubmission.as_view(form_class=AsyncFormBuilder), name='djangocms_forms_submissions'),
]

@mishbahr
Copy link
Owner

Let me know if the above suggestion solves your issue, so I can close this.
If you find this package useful, don't forget to ⭐ this repo :-)

@Dean-Christian-Armada
Copy link
Author

Dean-Christian-Armada commented Jul 22, 2017

Hello mishbahr,

Great package you have here, it's really helpful. I'll try your solution above please don't close this issue yet as I might add a solution of mine as well for others to use

Regards,
Dean

@Dean-Christian-Armada
Copy link
Author

Dean-Christian-Armada commented Jul 24, 2017

Hi guys,

Just in case you encountered this, my solution was to use django celery's backend email(django-celery-email). By that, e-mail will be sending asynchronously and you don't need to configure anything related to this plugin..

But I would suggest to @mishbahr , to make an option at least to make the e-mail asynchronously sent. Using the code below:

import threading

class EmailThread(threading.Thread):
    '''
    @brief      Avoids the usual delay on the backend when sending an e-mail
    '''

    def __init__(self, subject, body, from_email, recipient_list,
                 fail_silently, html_message):
        self.subject = subject
        self.body = body
        self.recipient_list = recipient_list
        self.from_email = from_email
        self.fail_silently = fail_silently
        self.html_message = html_message
        threading.Thread.__init__(self)

    def run(self):
        msg = EmailMultiAlternatives(self.subject, self.body, self.from_email,
                                     self.recipient_list)
        if self.html_message:
            msg.attach_alternative(self.html_message, "text/html")
        msg.send(self.fail_silently)

def send_mail(subject, body, from_email, recipient_list, fail_silently=False,
              html_message=None, *args, **kwargs):
    '''
    @brief
        - to replace django send mail which is synchronous and costs time
        in the process
    '''

    from . classes import EmailThread  # to avoid import conflict in model
    EmailThread(subject, body, from_email, recipient_list, fail_silently,
                html_message).start()

send_mail('Sample Subject', 'Sample Body', '[email protected]', '[email protected]', fail_silently=False, html_message='<h1>Hello World</h1>')

Thumbs up with this plug-in by the way!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants