from django.contrib.staticfiles.storage import staticfiles_storage from twilio.rest import Client from django.core.mail import EmailMessage, EmailMultiAlternatives from anymail.message import attach_inline_image_file from django.conf import settings from django.template.loader import get_template from project.celery import app @app.task def send_email(subject, to_email, template_name, attachments=[], inline_images=[], **kwargs): if inline_images: email = EmailMultiAlternatives(subject, '', to=[to_email], attachments=attachments) context = kwargs for name, path in inline_images: cid = attach_inline_image_file(email, staticfiles_storage.path(path)) context[name] = cid html = get_template(template_name).render(context) email.attach_alternative(html, "text/html") else: html = get_template(template_name).render(kwargs) email = EmailMessage(subject, html, to=[to_email], attachments=attachments) email.content_subtype = 'html' email.send() def send_sms(message, phone): client = Client(settings.TWILIO_ACCOUNT, settings.TWILIO_TOKEN) client.messages.create(to=phone, from_=settings.TWILIO_FROM_PHONE, body=message)