栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用python 3.4回复电子邮件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用python 3.4回复电子邮件

花了我一段时间,但以下内容似乎有效:

def send_mail_multi(headers, text, msgHtml="", orig=None):    """    """    msg = MIMEMultipart('mixed')    # Create message container - the correct MIME type is multipart/alternative.    body = MIMEMultipart('alternative')    for k,v in headers.items():        if isinstance(v, list): v = ', '.join(v)        msg.add_header(k, v)    # Attach parts into message container.    # According to RFC 2046, the last part of a multipart message, in this case    # the HTML message, is best and preferred.    if orig is not None:        text, msgHtml2 = append_orig_text(text, msgHtml, orig, False)        # Fix subject        msg["Subject"] = "RE: "+orig["Subject"].replace("Re: ", "").replace("RE: ", "")        msg['In-Reply-To'] = orig["Message-ID"]        msg['References'] = orig["Message-ID"]#+orig["References"].strip()        msg['Thread-Topic'] = orig["Thread-Topic"]        msg['Thread-Index'] = orig["Thread-Index"]    body.attach(MIMEText(text, 'plain'))    if msgHtml != "":         body.attach(MIMEText(msgHtml2, 'html'))    msg.attach(body)    send_it(msg)def append_orig_text(text, html, orig, google=False):    """    Append each part of the orig message into 2 new variables    (html and text) and return them. Also, remove any     attachments. If google=True then the reply will be prefixed    with ">". The last is not tested with html messages...    """    newhtml = ""    newtext = ""    for part in orig.walk():        if (part.get('Content-Disposition') and part.get('Content-Disposition').startswith("attachment")): part.set_type("text/plain") part.set_payload("Attachment removed: %s (%s, %d bytes)"  %(part.get_filename(),   part.get_content_type(),   len(part.get_payload(depre=True)))) del part["Content-Disposition"] del part["Content-Transfer-Encoding"]        if part.get_content_type().startswith("text/plain"): newtext += "n" newtext += part.get_payload(depre=False) if google:     newtext = newtext.replace("n","n> ")        elif part.get_content_type().startswith("text/html"): newhtml += "n" newhtml += part.get_payload(depre=True).depre("utf-8") if google:     newhtml = newhtml.replace("n", "n> ")    if newhtml == "":        newhtml = newtext.replace('n', '<br/>')    return (text+'nn'+newtext, html+'<br/>'+newhtml)

该代码需要一点整理,但是Outlook可以正确显示它(带有“下一个/上一个”选项)。无需

From, Send, To,Subject
手动创建标题,只需附加工作内容即可。

希望这可以节省别人的时间



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/660750.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号