From 7fb27b840e2249812fe722513d433b7e5c390f6c Mon Sep 17 00:00:00 2001 From: Baran Ugur Date: Tue, 1 Jun 2021 16:23:31 +0300 Subject: [PATCH 1/2] Enable setting up headers for emails --- .gitignore | 3 ++- django_ses_plus/models.py | 5 +++-- django_ses_plus/tasks.py | 11 ++++++----- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 8bd847a..705abd0 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,5 @@ coverage.xml .coverage __pycache__ .DS_Store -*.sqlite* \ No newline at end of file +*.sqlite* +.vscode/ \ No newline at end of file diff --git a/django_ses_plus/models.py b/django_ses_plus/models.py index e63af36..f1df28c 100644 --- a/django_ses_plus/models.py +++ b/django_ses_plus/models.py @@ -37,7 +37,7 @@ class SendEmailMixin(object): def get_to_email(self): return self.email - def send_email(self, subject, template_path, context, from_email=None, language=None): + def send_email(self, subject, template_path, context, from_email=None, language=None, headers=None): from .tasks import send_email if not DJANGO_SES_PLUS_SETTINGS["SEND_EMAIL"]: return _("Email cannot be sent due to SEND_EMAIL flag in project settings.") @@ -56,7 +56,8 @@ def send_email(self, subject, template_path, context, from_email=None, language= to_email=self.get_to_email(), html_message=html_message, from_email=from_email, - recipient_id=recipient_id + recipient_id=recipient_id, + headers=headers ) if language: diff --git a/django_ses_plus/tasks.py b/django_ses_plus/tasks.py index dc19c88..7881438 100644 --- a/django_ses_plus/tasks.py +++ b/django_ses_plus/tasks.py @@ -5,28 +5,29 @@ from django_ses_plus import logger from .settings import DJANGO_SES_PLUS_SETTINGS from django.core.files.base import ContentFile -from django.core.mail import send_mail +from django.core.mail import EmailMultiAlternatives from django.utils.translation import ugettext_lazy as _ from .models import SentEmail @shared_task(retry_kwargs=DJANGO_SES_PLUS_SETTINGS["CELERY_TASK_RETRY_KWARGS"]) -def send_email(subject, to_email, html_message, from_email=None, message=None, recipient_id=None): +def send_email(subject, to_email, html_message, from_email=None, message=None, recipient_id=None, headers=None): if not DJANGO_SES_PLUS_SETTINGS["SEND_EMAIL"]: return _("Email cannot be sent due to SEND_EMAIL flag in project settings.") if not from_email: from_email = DJANGO_SES_PLUS_SETTINGS["DEFAULT_FROM_EMAIL"] - send_mail( + email = EmailMultiAlternatives( subject=subject, message=message, - html_message=html_message, from_email=from_email, recipient_list=[to_email], - fail_silently=False, + headers=headers ) + email.attach_alternative(html_message, 'text/html') + email.send() try: SentEmail.objects.create( From d5d2ecc2da06808db53ef592af3239a57c8996dc Mon Sep 17 00:00:00 2001 From: Baran Ugur Date: Tue, 1 Jun 2021 18:33:24 +0300 Subject: [PATCH 2/2] Fix constructor kwargs for EmailMultiAlternatives --- django_ses_plus/tasks.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/django_ses_plus/tasks.py b/django_ses_plus/tasks.py index 7881438..9d10613 100644 --- a/django_ses_plus/tasks.py +++ b/django_ses_plus/tasks.py @@ -21,9 +21,9 @@ def send_email(subject, to_email, html_message, from_email=None, message=None, r email = EmailMultiAlternatives( subject=subject, - message=message, + body=message, from_email=from_email, - recipient_list=[to_email], + to=[to_email], headers=headers ) email.attach_alternative(html_message, 'text/html')