2 patches - minor improvements - for viewmailattachments.py
First of all - many thanks for your useful solution! Saved me many hours of trying to implement the same thing from scratch (I would go with Perl though) ))))
First patch is to name the tab in browser after the subject and "from" headers, additionally it embeds the small "header-like" set of lines into html which is shown (From/Sent/Cc/Subject) making navigation in several tabs more comfortable.
diff --git a/bin/viewmailattachments.py b/bin/viewmailattachments.py
index 6117106..ba0366b 100755
--- a/bin/viewmailattachments.py
+++ b/bin/viewmailattachments.py
@@ -1,4 +1,4 @@
-#! /usr/bin/env python
+#! /usr/bin/env python3
#
# ans: borrowed from 'wget
# https://raw.githubusercontent.com/akkana/scripts/master/viewmailattachments.py'
@@ -34,6 +34,7 @@ from email.parser import BytesParser
from email.policy import default as default_policy
import subprocess
from collections import OrderedDict # for python < 3.7
+import html
################################################
@@ -65,7 +66,7 @@ BROWSERS = OrderedDict([
"--basedir", "/tmp/mailattachments",
"-s", "content.dns_prefetch", "false",
"-s", "content.javascript.enabled", "false" ],
- # If using PDFJS, add: "-s", "content.javascript.enabled", "false"
+ # If using PDFJS, add: "-s", "content.javascript.enabled", "false"
'ARGS': [ "--target", "tab-bg",
"--basedir", "/tmp/mailattachments",
# Don't need to specify privacy, prefetch or JS
@@ -198,6 +199,41 @@ def sanitize_filename(badstr):
return filename
+def nvl(str_or_nothing, def_ret):
+ if (str_or_nothing):
+ return html.escape(str_or_nothing)
+ else:
+ return def_ret;
+
+def create_headers_block(msg):
+ ret = ("<p>"
+ + "<b>From: </b>" + nvl(msg.get("from") , "") + "<br>"
+ + "<b>Sent: </b>" + nvl(msg.get("date") , "") + "<br>"
+ + "<b>To: </b>" + nvl(msg.get("to") , "") + "<br>"
+ + "<b>Cc: </b>" + nvl(msg.get("cc") , "") + "<br>"
+ + "<b>Subject: </b>" + nvl(msg.get("subject"), "") + "</p>")
+ return ret
+
+def decorate_html_with_headers(htmlsrc, msg):
+ ret = (("<html><head><title>"
+ + nvl(msg.get("subject"),"")
+ + "("
+ + nvl(msg.get("from"),"")
+ + ")"
+ + "</title></head>").encode("utf-8")
+ + create_headers_block(msg).encode("utf-8")
+ + htmlsrc
+ + "</html>".encode("utf-8"))
+ return ret
+
+ #ret = ("<html><head><title>"
+ # + str(msg.get("subject"))
+ # + "("
+ # + str(msg.get("from"))
+ # + ")"
+ # + "</title></head>"
+ # + htmlsrc
+ # + "</html>")
def view_html_message(f, tmpdir):
# Note: the obvious way to read a message is
@@ -386,6 +422,7 @@ def view_html_message(f, tmpdir):
embedded_parts.append(sf_cid)
htmlsrc = newhtmlsrc
+ htmlsrc = decorate_html_with_headers(htmlsrc, msg)
fp.write(htmlsrc)
fp.close()
if DEBUG:
While on that, (needed if "-d" is specified) second patch adds a way to interpret special file name "-" as synonym for STDIN.
diff --git a/bin/viewmailattachments.py b/bin/viewmailattachments.py
index 1730d1b..6117106 100755
--- a/bin/viewmailattachments.py
+++ b/bin/viewmailattachments.py
@@ -208,6 +208,9 @@ def view_html_message(f, tmpdir):
# but to get that you need the more complicated BytesParser method below.
# The policy argument to BytesParser is mandatory: without it,
# again, you'll get a Message and not an EmailMessage.
+ if f == "-":
+ f = None
+
if f:
if os.path.isdir(f):
# Maildir: f is a maildir like /tmp/mutttmpbox,
@@ -260,6 +263,8 @@ def view_html_message(f, tmpdir):
print()
if DEBUG:
+ print("subj:", msg.get("subject"))
+ print("from:", msg.get("from"))
print_structure(msg)
for part in msg.walk():
@@ -499,6 +504,7 @@ if __name__ == '__main__':
for f in sys.argv[1:]:
if f == '-d':
DEBUG = True
+ print ("Tempdir: ", tmpdir)
continue
view_html_message(f, tmpdir)
else:
Good changes, thanks! One thing: it seems to me that the important thing your nvl() function is doing is the HTML escape, not the empty-string-if-null, so I'd prefer to call it html_escape or html_safe or something like that. Do you disagree or have an alternate suggestion? Otherwise the patch looks great.
html_safe sounds most appropriate to me, thanks for your time !