Created send_plain_email_task celery task and calling it on contact send

This commit is contained in:
PCoder 2017-08-27 15:23:19 +05:30
parent d69dbcb720
commit 1da04a05d1
2 changed files with 30 additions and 0 deletions

View file

@ -21,6 +21,7 @@ from opennebula_api.models import OpenNebulaManager
from opennebula_api.serializers import VirtualMachineTemplateSerializer, \
VMTemplateSerializer
from datacenterlight.tasks import create_vm_task
from utils.tasks import send_plain_email_task
class ContactUsView(FormView):
@ -41,6 +42,15 @@ class ContactUsView(FormView):
def form_valid(self, form):
form.save()
email_data = {
'subject': 'Request received on Data Center Light',
'from_email': settings.DCL_SUPPORT_FROM_ADDRESS,
'to': ['info@ungleich.ch'],
'body': "\n".join(
["%s=%s" % (k, v) for (k, v) in form.cleaned_data.items()]),
'reply_to': [form.cleaned_data.get('email')],
}
send_plain_email_task.delay(email_data)
if self.request.is_ajax():
return self.render_to_response(
self.get_context_data(success=True, contact_form=form))

20
utils/tasks.py Normal file
View file

@ -0,0 +1,20 @@
from celery.utils.log import get_task_logger
from django.conf import settings
from dynamicweb.celery import app
from django.core.mail import EmailMessage
logger = get_task_logger(__name__)
@app.task(bind=True, max_retries=settings.CELERY_MAX_RETRIES)
def send_plain_email_task(self, email_data):
"""
This is a generic celery task to be used for sending emails.
A celery wrapper task for EmailMessage
:param self:
:param email_data: A dict of all needed email headers
:return:
"""
email = EmailMessage(**email_data)
email.send()