utils.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:m2mb 作者: thperret 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号