Skip to content

Commit

Permalink
Merge pull request #84 from SparkPost/ISSUE-74
Browse files Browse the repository at this point in the history
Added support for attachments to Django backend
  • Loading branch information
richleland committed Mar 10, 2016
2 parents 0aa08ed + 816f5f5 commit 72f117b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
13 changes: 9 additions & 4 deletions sparkpost/django/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,14 @@ def __init__(self, message):
)

if message.attachments:
raise UnsupportedContent(
'The SparkPost Django email backend does not '
'currently support attachment.'
)
formatted['attachments'] = []
for attachment in message.attachments:
filename, content, mimetype = attachment
formatted['attachments'].append({
'name': filename,
'data': content,
'type': mimetype
})
print(message.attachments)

return super(SparkPostMessage, self).__init__(formatted)
25 changes: 13 additions & 12 deletions test/django/test_message.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
try:
from StringIO import StringIO
except ImportError:
from io import StringIO

import pytest
from django.core.mail import EmailMultiAlternatives
from django.core.mail.message import EmailMessage

from sparkpost.django.exceptions import UnsupportedContent
from sparkpost.django.message import SparkPostMessage
from .utils import at_least_version

Expand Down Expand Up @@ -66,13 +59,21 @@ def test_cc_bcc():


def test_attachment():
attachment = StringIO()
attachment.write('hello file')
email_message = EmailMessage(**base_options)
email_message.attach('file.txt', attachment, 'text/plain')
email_message.attach('file.txt', 'test content', 'text/plain')

with pytest.raises(UnsupportedContent):
SparkPostMessage(email_message)
actual = SparkPostMessage(email_message)
expected = dict(
attachments=[
{
'name': 'file.txt',
'data': 'test content',
'type': 'text/plain'
}
]
)
expected.update(base_expected)
assert actual == expected

if at_least_version('1.8'):
def test_reply_to():
Expand Down

0 comments on commit 72f117b

Please sign in to comment.