def format_mail(loop, msg, to_text=True, ignore_tables=True):
"""Format the mail to markdown
Parameter
---------
msg: email.message
to_text: bool, optional
Convert text/html mails to text/plain with markdown formatting
Returns
-------
text: str
"""
h = html2text.HTML2Text()
h.ignore_tables = ignore_tables
body = None
for part in msg.walk():
if to_text and part.get_content_type() == "text/html":
body = h.handle(quopri.decodestring(part.get_payload()).decode())
break
elif part.get_content_type() == "text/plain":
body = quopri.decodestring(part.get_payload())
break
if not body:
log.error("Could not find text body mail")
body = quopri.decodestring(msg.as_string())
text = f"### {msg['Subject']} \n {body}"
return text
评论列表
文章目录