Skip to content

Commit

Permalink
Merge pull request #131 from modoboa/fix/empty_mail_display
Browse files Browse the repository at this point in the history
Fixed empty email display
  • Loading branch information
tonioo committed Nov 17, 2017
2 parents 9fc22f3 + 1f6fa76 commit fb4d840
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
5 changes: 4 additions & 1 deletion modoboa_webmail/lib/imapemail.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,14 @@ def body(self):
if self._body is None:
self.fetch_body_structure()
bodyc = u""
for part in self.bs.contents[self.mformat]:
parts = self.bs.contents.get(self.mformat, [])
for part in parts:
pnum = part["pnum"]
data = self.imapc._cmd(
"FETCH", self.mailid, "(BODY.PEEK[%s])" % pnum
)
if not data:
continue
content = decode_payload(
part["encoding"], data[int(self.mailid)]["BODY[%s]" % pnum]
)
Expand Down
6 changes: 6 additions & 0 deletions modoboa_webmail/tests/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,9 @@
(b'1 (UID 947 BODYSTRUCTURE ("text" "html" ("charset" "utf-8") NIL NIL "8bit" 889 34 NIL NIL NIL NIL) BODY[HEADER.FIELDS (FROM TO CC DATE SUBJECT)] {80}', 'From: Antoine Nguyen <tonio@ngyn.org>\r\nDate: Sat, 26 Mar 2016 11:45:49 +0100\r\n\r\n'),
b')'
]

BODYSTRUCTURE_EMPTY_MAIL = [
b'33 (UID 33 BODYSTRUCTURE (("text" "plain" ("charset" "us-ascii") NIL NIL "quoted-printable" 0 0 NIL NIL NIL NIL)("application" "zip" ("name" "hotmail.com!ngyn.org!1435428000!1435514400.zip") NIL NIL "base64" 978 NIL ("attachment" ("filename" "hotmail.com!ngyn.org!1435428000!1435514400.zip")) NIL NIL) "mixed" ("boundary" "--boundary_32281_b52cf564-2a50-4f96-aeb0-ef5f83b05463") NIL NIL NIL))'
]

EMPTY_BODY = [(b'33 (UID 33 BODY[1] {0}', b''), b')']
16 changes: 14 additions & 2 deletions modoboa_webmail/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,20 @@ def uid(self, command, *args):
if command == "SORT":
return "OK", [b"19"]
elif command == "FETCH":
uid = args[0]
uid = int(args[0])
data = BODYSTRUCTURE_SAMPLE_WITH_FLAGS
if int(uid) == 46931:
if uid == 46931:
if args[1] == "(BODYSTRUCTURE)":
data = tests_data.BODYSTRUCTURE_ONLY_4
elif "HEADER.FIELDS" in args[1]:
data = tests_data.BODYSTRUCTURE_SAMPLE_4
else:
data = tests_data.BODY_PLAIN_4
elif uid == 33:
if args[1] == "(BODYSTRUCTURE)":
data = tests_data.BODYSTRUCTURE_EMPTY_MAIL
else:
data = tests_data.EMPTY_BODY
return "OK", data
elif command == "STORE":
return "OK", []
Expand Down Expand Up @@ -311,3 +316,10 @@ def test_reply_to_email(self):
self.assertEqual(
mail.outbox[0].from_email, "user@test.com")
self.assertIn("References", mail.outbox[0].extra_headers)

def test_getmailcontent_empty_mail(self):
"""Try to display an empty email."""
url = "{}?action=reply&mbox=INBOX&mailid=33".format(
reverse("modoboa_webmail:mailcontent_get"))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
2 changes: 1 addition & 1 deletion modoboa_webmail/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ def getmailcontent(request):
email = ImapEmail(
request,
"%s:%s" % (mbox, mailid), dformat="DISPLAYMODE",
links=int(request.GET["links"])
links=int(request.GET.get("links", "0"))
)
return render(request, "common/viewmail.html", {
"mailbody": email.body if email.body else ""
Expand Down

0 comments on commit fb4d840

Please sign in to comment.