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

Refs #54, #55 -- check payload type before trimming #59

Merged
merged 4 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 6 additions & 3 deletions emark/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,12 @@ class ConsoleEmailBackendMixin:

def write_message(self, message):
msg = message.message()
payload_count = len(msg.get_payload())
if payload_count > 1:
msg.set_payload(msg.get_payload(0))
original_payload = msg.get_payload()
payload_count = len(original_payload)
if not msg.is_multipart():
return super().write_message(message)

msg.set_payload(msg.get_payload(0))
msg_data = msg.as_bytes()
charset = (
msg.get_charset().get_output_charset() if msg.get_charset() else "utf-8"
Expand Down
9 changes: 8 additions & 1 deletion tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@


class TestConsoleEmailBackend:
def test_write_message(self):
def test_write_message__single(self):
msg = EmailMessage(body="foo\nbar")
with io.StringIO() as stream:
backends.ConsoleEmailBackend(stream=stream).write_message(msg)
stdout = stream.getvalue()
assert "foo\nbar" in stdout

def test_write_message__multipart(self):
msg = EmailMultiAlternatives(body="foo")
msg.attach_alternative("<html></html>", "text/html")
with io.StringIO() as stream:
Expand Down