diff --git a/docs/django/backend.rst b/docs/django/backend.rst index 1f7d511..78c5140 100644 --- a/docs/django/backend.rst +++ b/docs/django/backend.rst @@ -44,11 +44,11 @@ Django is now configured to use the SparkPost email backend. You can now send ma html_message='

Hello Rock stars!

', ) - + You can also use `EmailMessage` or `EmailMultiAlternatives` class directly. That will give you access to more specific fileds like `template`: .. code-block:: python - + email = EmailMessage( to=[ { @@ -83,6 +83,60 @@ Or cc, bcc, reply to, or attachments fields: email.attach('image.png', img_data, 'image/png') email.send() +Metadata Email Sending (new as of July 31, 2018) +------------------------------------------------ + +If you are trying to attach metadata for SparkPost webhook usage, you need to use `EmailMessageWithMetadata` or `EmailMultiAlternativesWithMetadata` class directly. That will give you access to more specific fileds like `template`: + +.. code-block:: python + + from sparkpost.django.message import EmailMessageWithMetadata + + email = EmailMessageWithMetadata( + to=[ + { + "address": "to@example.com", + "substitution_data": { + "key": "value" + } + } + ], + from_email='test@from.com', + metadata={'key1': 'value1'} + ) + email.template = 'template-id' + + # Add more metadata + email.metadata['key2': 'value2'] + + email.send() + + +Or cc, bcc, reply to, or attachments fields: + +.. code-block:: python + + from sparkpost.django.message import EmailMultiAlternativesWithMetadata + + email = EmailMultiAlternatives( + subject='hello from sparkpost', + body='Woo hoo! Sent from Django!', + from_email='from@yourdomain.com', + to=['to@example.com'], + cc=['ccone@example.com'], + bcc=['bccone@example.com'], + reply_to=['replyone@example.com'], + metadata={'key1': 'value1'} + ) + + email.attach_alternative('

Woo hoo! Sent from Django!

', 'text/html') + email.attach('image.png', img_data, 'image/png') + + # Add more metadata if we want + email.metadata['key2': 'value2'] + + email.send() + Supported version ----------------- diff --git a/examples/django/send_email_with_metadata.py b/examples/django/send_email_with_metadata.py new file mode 100644 index 0000000..8369e75 --- /dev/null +++ b/examples/django/send_email_with_metadata.py @@ -0,0 +1,53 @@ +from sparkpost.django.message import EmailMessageWithMetadata, \ + EmailMultiAlternativesWithMetadata + + +def send_django_email_with_metadata(): + """ + Make sure you setup your Django Email Backend before trying these examples: + https://github.com/SparkPost/python-sparkpost/blob/master/docs/django/backend.rst + + EmailMessageWithMetadata is just a simple update to Django's EmailMessage + (https://docs.djangoproject.com/en/2.0/topics/email/#django.core.mail.EmailMessage) + Which adds a metadata field. The metadata can be sent back to you in + webhook POSTs from SparkPost. Without these classes, The SparkPost Django + email backend will not be able to send metadata with the email. + """ + + email = EmailMessageWithMetadata( + 'Hello', + 'Body goes here', + 'from@example.com', + ['to1@example.com', 'to2@example.com'], + ['bcc@example.com'], + reply_to=['another@example.com'], + headers={'Message-ID': 'foo'}, + metadata={'some_metadata': 'that you want'} + ) + + email.metadata['more_metadata'] = 'true' + + email.send(fail_silently=False) + + +def send_django_email_alternatives_with_metadata(): + """ + Same as send_django_email_with_metadata(), but using + EmailMultiAlternativesWithMetadata. See send_django_email_with_metadata() + doc string for more details + """ + email = EmailMultiAlternativesWithMetadata( + 'Hello', + 'Body goes here', + 'from@example.com', + ['to1@example.com', 'to2@example.com'], + ['bcc@example.com'], + reply_to=['another@example.com'], + headers={'Message-ID': 'foo'},) + + email.metadata['more_metadata'] = 'true' + + html_content = '

This is an important message.

' + email.attach_alternative(html_content, "text/html") + + email.send(fail_silently=False) diff --git a/sparkpost/django/message.py b/sparkpost/django/message.py index 9b12222..870dfc9 100644 --- a/sparkpost/django/message.py +++ b/sparkpost/django/message.py @@ -1,13 +1,48 @@ import mimetypes from base64 import b64encode -from django.core.mail import EmailMultiAlternatives +from django.core.mail import EmailMultiAlternatives, EmailMessage from django.core.mail.message import DEFAULT_ATTACHMENT_MIME_TYPE from django.conf import settings from .exceptions import UnsupportedContent +class EmailMultiAlternativesWithMetadata(EmailMultiAlternatives): + + def __init__(self, subject='', body='', from_email=None, to=None, + bcc=None, connection=None, attachments=None, headers=None, + alternatives=None, cc=None, reply_to=None, metadata=None): + + super(EmailMultiAlternativesWithMetadata, self + ).__init__(subject=subject, body=body, from_email=from_email, + to=to, bcc=bcc, connection=connection, + attachments=attachments, headers=headers, + alternatives=alternatives, cc=cc, reply_to=reply_to) + + if metadata is None: + self.metadata = {} + else: + self.metadata = metadata + + +class EmailMessageWithMetadata(EmailMessage): + + def __init__(self, subject='', body='', from_email=None, to=None, bcc=None, + connection=None, attachments=None, headers=None, cc=None, + reply_to=None, metadata=None): + + super(EmailMessageWithMetadata, self).__init__( + subject=subject, body=body, from_email=from_email, to=to, bcc=bcc, + connection=connection, attachments=attachments, headers=headers, + cc=cc, reply_to=reply_to) + + if metadata is None: + self.metadata = {} + else: + self.metadata = metadata + + class SparkPostMessage(dict): """ Takes a Django EmailMessage and formats it for use with the SparkPost API. @@ -98,4 +133,8 @@ def __init__(self, message): if msys_api and msys_api.get('options', {}).get('transactional', False): # noqa: E501 formatted['transactional'] = True + # Handle metadata if it exists + if hasattr(message, 'metadata'): + formatted['metadata'] = message.metadata + super(SparkPostMessage, self).__init__(formatted)