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

Added handling singlepart emails #8

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
10 changes: 10 additions & 0 deletions redbox/tests/examples/INBOX/3.eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
From: [email protected]
MIME-Version: 1.0
Date: Wed, 22 Mar 2023 06:44:05 -0700
Message-ID: <[email protected]>
Subject: redacted subject
To: [email protected]
Content-Type: text/plain; charset="UTF-8"

line 1
line 2
38 changes: 33 additions & 5 deletions redbox/tests/test_fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

def test_fetch(IMAP4):
box = EmailBox(host="localhost", port=0, cls_imap=DummyGmailImap)

msg = EmailMessage(uid=1, session=box.inbox.session, mailbox="MYBOX")
assert msg.content == (ROOT / "MYBOX/1.eml").read_text()
assert isinstance(msg.email, Message)
Expand Down Expand Up @@ -49,7 +49,7 @@ def test_fetch(IMAP4):

def test_set_flags(IMAP4):
box = EmailBox(host="localhost", port=0, cls_imap=DummyGmailImap)

msg = EmailMessage(uid=1, session=box.inbox.session, mailbox="MYBOX")
assert msg.seen
assert msg.flagged
Expand All @@ -62,12 +62,12 @@ def test_set_flags(IMAP4):

def test_set_flags_methods(IMAP4):
box = EmailBox(host="localhost", port=0, cls_imap=DummyGmailImap)

msg = EmailMessage(uid=2, session=box.inbox.session, mailbox="MYBOX")
assert not msg.seen
assert not msg.flagged
assert not msg.deleted

msg.read()
msg.delete()
msg.flag()
Expand All @@ -80,4 +80,32 @@ def test_set_flags_methods(IMAP4):
msg.unflag()
assert not msg.seen
assert not msg.flagged
assert not msg.deleted
assert not msg.deleted



def test_single_part_text_plain(IMAP4):
box = EmailBox(host="localhost", port=0, cls_imap=DummyGmailImap)

msg = EmailMessage(uid=3, session=box.inbox.session, mailbox="INBOX")

assert msg.subject == "redacted subject"
assert msg.text_body == dedent("""
line 1
line 2
"""
)[1:]

assert msg.html_body is None


def test_single_part_text_html(IMAP4):
box = EmailBox(host="localhost", port=0, cls_imap=DummyGmailImap)

msg = EmailMessage(uid=2, session=box.inbox.session, mailbox="INBOX")

assert msg.text_body is None
assert msg.html_body.strip() == dedent("""
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><div dir="ltr"><br></div></body></html>
""").strip()
18 changes: 13 additions & 5 deletions redbox/utils/inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
from email.message import EmailMessage
from redbox.models.attachment import Attachment

class Inspector:

def __init__(self, msg:EmailMessage):
class Inspector:
def __init__(self, msg: EmailMessage):
self.message = msg

def get_headers(self) -> Dict[str, str]:
return dict(self.message.items())

def get_html_body(self) -> str:
if not self.message.is_multipart():
if "text/html" == self.message.get_content_type():
return self.message.get_payload()
return None
for pl in self.message.get_payload():
content_type = pl['Content-Type'].split(";")
content_type = pl["Content-Type"].split(";")
if "text/html" in content_type:
return pl.get_payload()
elif "multipart/related" in content_type:
Expand All @@ -21,8 +25,12 @@ def get_html_body(self) -> str:
return Inspector(pl).get_html_body()

def get_text_body(self) -> str:
if not self.message.is_multipart():
if "text/plain" == self.message.get_content_type():
return self.message.get_payload()
return None
for pl in self.message.get_payload():
content_type = pl['Content-Type'].split(";")
content_type = pl["Content-Type"].split(";")
if "text/plain" in content_type:
return pl.get_payload()
elif "multipart/related" in content_type:
Expand All @@ -39,6 +47,6 @@ def get_attachments(self) -> List:
# text/plain: For textual files
# application/octet-stream: For all others
for pl in self.message.get_payload():
content_type = pl['Content-Type'].split(";")[0]
content_type = pl["Content-Type"].split(";")[0]
if content_type.startswith("application/"):
yield Attachment.from_message(pl)