leahneukirchen / mblaze

Unix utilities to deal with Maildir
Other
441 stars 48 forks source link

mhtml: Simple script to view HTML emails in browser #166

Open Anachron opened 5 years ago

Anachron commented 5 years ago
#!/bin/sh

msg="${1:-$(mseq)}"
tf=$(mktemp '/tmp/mhtml.XXXXXX.html')
hp=$(mshow -t "${msg}" | grep 'text/html' | cut -d ':' -f 1 | xargs)
mshow -O "${msg}" "${hp}" | sed 's/^[[:blank:]]*//;s/[[:blank:]]*$//' | sed '/^$/d' > "${tf}"
"${BROWSER:-xdg-open}" "${tf}"
sleep 1
rm "${tf}"
Duncaen commented 5 years ago

This breaks if there are multiple html parts.

You could use something like the following to use the alternative choosing thing instead of all attachments, but I think this would still create wrong html files if there are multiple parts with text/html alternatives and they all contain the doctype and head nodes.

#!/bin/sh
tmp=$(mktemp '/tmp/mhtml.XXXXXX.html')
mshow -NA text/html -h "" ${1} | tail -n +2 >"$tmp" 
Anachron commented 5 years ago

Yeah I'm aware of that, the script I posted above works for at least 9/10 html-only emails I receive so that was fair enough for such a short script I thought.

I'll think of a good way how to handle multi-html-parts.

anjandev commented 4 years ago

Hi @Anachron, thank you for sharing your approach for opening html emails. I built upon your example for an approach that works for me:

ATTACH="$(mshow -t . | dmenu)"
MIME="$(echo "$ATTACH" | cut -f 2 -d ":" | cut -f 2 -d " ")"
ATTACHNUM="$(echo "$ATTACH" | cut -f1 -d':')"

tmp="$(mktemp '/tmp/mblaze.XXXXXX')"
mshow -O . "$ATTACHNUM" > "$tmp"
# replace this with xdg?
case "$MIME" in
    "application/pdf")
        zathura "$tmp"
        ;;
    "text/html")
        firefox "$tmp" 
        ;;
# More stuff for pngs and stuff?
esac

You can change dmenu to fzf or any other fuzzy finder. I use this snippet as a general tool for extracting all attachments in emails in my email script I made for my pinephone:

https://git.sr.ht/~anjan/sxmo-userscripts/tree/master/mail.sh#L79

I dont know if my approach is the best but it works for emails with multi html and works as a general attachment extractor. It would be cool to have an attachment extractor included in mblaze.