scripts/input_to_browser.py

51 lines
1.3 KiB
Python
Raw Normal View History

2024-09-10 15:21:37 +00:00
import alot
2024-02-28 10:04:05 +00:00
import tempfile
import webbrowser
2024-09-10 15:21:37 +00:00
from alot.helper import string_sanitize
from alot.helper import string_decode
# Helper method to extract the raw html part of a message. Note that it
# only extracts the first text/html part found.
def _get_raw_html(msg):
mail = msg.get_email()
for part in mail.walk():
ctype = part.get_content_type()
if ctype != "text/html":
continue
cd = part.get('Content-Disposition', '')
if cd.startswith('attachment'):
continue
enc = part.get_content_charset() or 'utf-8'
raw = string_decode(part.get_payload(decode=True), enc)
return string_sanitize(raw), enc
return None, None
2024-02-28 10:04:05 +00:00
2024-09-10 15:21:37 +00:00
# Opens HTML emails in an external browser.
# Related issue:
# - https://github.com/pazz/alot/issues/1153
def open_in_browser(ui=None):
ui.notify("Opening message in browser...")
msg = ui.current_buffer.get_selected_message()
2024-02-28 10:04:05 +00:00
2024-09-10 15:21:37 +00:00
htmlstr, enc = _get_raw_html(msg)
2024-02-28 10:04:05 +00:00
2024-09-10 15:21:37 +00:00
if htmlstr == None:
ui.notify("Email has no html part")
return
2024-02-28 10:04:05 +00:00
2024-09-10 15:21:37 +00:00
temp = tempfile.NamedTemporaryFile(prefix="alot-",suffix=".html",
delete=False)
temp.write(htmlstr.encode(enc))
temp.flush()
temp.close()
webbrowser.open(temp.name)