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

创建带有嵌入式图像和PDF附件的HTML邮件

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

创建带有嵌入式图像和PDF附件的HTML邮件

我对这种结构进行了反向工程,以便在实践中使用:

+-------------------------------------------------------+| multipart/mixed      ||||  +-------------------------------------------------+  ||  |   multipart/related       |  ||  |     |  ||  |  +-------------------------------------------+  |  ||  |  | multipart/alternative          |  |  ||  |  |          |  |  ||  |  |  +-------------------------------------+  |  |  ||  |  |  | text can contain [cid:logo.png]     |  |  |  ||  |  |  +-------------------------------------+  |  |  ||  |  |          |  |  ||  |  |  +-------------------------------------+  |  |  ||  |  |  | html can contain src="cid:logo.png" |  |  |  ||  |  |  +-------------------------------------+  |  |  ||  |  |          |  |  ||  |  +-------------------------------------------+  |  ||  |     |  ||  |  +-------------------------------------------+  |  ||  |  | image logo.png  "inline" attachment       |  |  ||  |  +-------------------------------------------+  |  ||  |     |  ||  +-------------------------------------------------+  ||||  +-------------------------------------------------+  ||  | pdf ("download" attachment, not inline)         |  ||  +-------------------------------------------------+  |||+-------------------------------------------------------+

不幸的是,我只发现了这个复杂的解决方案:

from django.core.mail.message import EmailMessagedef create_email(subject='', body='', from_email=None, to=None, bcc=None,      connection=None, attachments=[], headers=None,      cc=None, reply_to=None, html_body='', html_inline_attachments=[]):    message = _create_email(subject=subject, body=body, from_email=from_email, to=to, bcc=bcc,      connection=connection, headers=headers, cc=cc, reply_to=reply_to,      html_body=html_body, html_inline_attachments=html_inline_attachments)    for attachment in attachments:        if isinstance(attachment, basestring): message.attach_file(attachment) continue        message.attach(attachment)    return messagedef _create_email(subject='', body='', from_email=None, to=None, bcc=None,       connection=None, headers=None,       cc=None, reply_to=None, html_body='', html_inline_attachments=[]):    if not (body or html_body):        raise ValueError('Missing body or html_body!')    for address, type, name in [        (from_email, basestring, 'from_email'),        (to, list, 'to'),        (cc, list, 'cc'),        (bcc, list, 'bcc')]:        if address and not isinstance(address, type): raise ValueError('"{}" must be a list! ({})'.format(name, address))    if body and not html_body:        if html_inline_attachments: raise ValueError('"html_body" is missing!')


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

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

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