Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable setting up headers for emails #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ coverage.xml
.coverage
__pycache__
.DS_Store
*.sqlite*
*.sqlite*
.vscode/
5 changes: 3 additions & 2 deletions django_ses_plus/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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:
Expand Down
15 changes: 8 additions & 7 deletions django_ses_plus/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
body=message,
from_email=from_email,
recipient_list=[to_email],
fail_silently=False,
to=[to_email],
headers=headers
)
email.attach_alternative(html_message, 'text/html')
email.send()

try:
SentEmail.objects.create(
Expand Down