Skip to content

Commit 74d9a03

Browse files
committed
Simplify admin preview of emails
Use the python3 function to get the plaintext body of the email, instead of our own very limited one we had before.
1 parent 60f75e4 commit 74d9a03

File tree

1 file changed

+3
-11
lines changed

1 file changed

+3
-11
lines changed

pgweb/mailqueue/admin.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from django.contrib import admin
22

33
from email.parser import Parser
4+
from email import policy
45

56
from .models import QueuedMail
67

@@ -13,18 +14,9 @@ def parsed_content(self, obj):
1314
# We only try to parse the *first* piece, because we assume
1415
# all our emails are trivial.
1516
try:
16-
parser = Parser()
17+
parser = Parser(policy=policy.default)
1718
msg = parser.parsestr(obj.fullmsg)
18-
b = msg.get_payload(decode=True)
19-
if b:
20-
return b.decode('utf8')
21-
22-
pl = msg.get_payload()
23-
for p in pl:
24-
b = p.get_payload(decode=True)
25-
if b:
26-
return b.decode('utf8')
27-
return "Could not find body"
19+
return msg.get_body(preferencelist=('plain', )).get_payload(decode=True).decode('utf8')
2820
except Exception as e:
2921
return "Failed to get body: %s" % e
3022

0 commit comments

Comments
 (0)