小编典典

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

django

我想在Python / Django中编写包含以下部分的HTML邮件:

  • HTML链接到logo.png
  • logo.png应该在邮件用户代理中内联显示(不作为附件显示)
  • info.pdf,应显示为附件
  • 如果邮件用户代理无法显示HTML,则应显示的文本。
    我关注了这篇博客文章。

结果:

  • HTML和内嵌图片有效
  • 但info.pdf文件的处理方式类似于内联logo.png,并且某些邮件用户代理未显示它:-(
    如何在python中的一封邮件中同时创建两种方式(下载(info.pdf)和内联(logo.png))?

阅读 416

收藏
2020-04-01

共1个答案

小编典典

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

+-------------------------------------------------------+
| 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 EmailMessage


def 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 message


def _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!')
2020-04-01