使用内联图像和PDF附件创建HTML邮件 [英] Create HTML Mail with inline Image and PDF Attachment

查看:115
本文介绍了使用内联图像和PDF附件创建HTML邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在包含这些部分的Python / Django中编写一个HTML邮件:

I want to write a HTML mail in Python/Django containing these parts:


  • HTML链接到logo.png

  • logo.png应该在邮件用户代理

  • info.pdf中显示为内联(不作为附件),应该显示为附件

  • 如果邮件用户代理无法显示HTML,应显示的文本。

  • HTML linking to logo.png
  • logo.png which should be displayed inline (not as attachment) in the mail user agent
  • info.pdf which should be displayed as attachment
  • Text which should be displayed if the mail user agent can't display HTML.

我遵循这个博客文章。

结果:


  • HTML和内嵌图片的作品

  • ,但info.pdf文件被视为inline logo.png,一些邮件用户代理不显示: - (

如何在python的一个邮件中创建两种方式(download(info.pdf)和inline(logo.png))?

How to create both ways (download (info.pdf) and inline (logo.png)) in one mail in python?

推荐答案

我反向设计,这个结构在p中被使用ractice:

I reverse engineered that this structure gets used in practice:

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

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

Unfortunately I only found this complicated solution:

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!')
        return EmailMessage(subject=subject, body=body, from_email=from_email, to=to, bcc=bcc,
                            connection=connection, headers=headers, cc=cc,
                            reply_to=reply_to)

    if not body:
        body = html_to_text(html_body)
    msg = EmailMessage(subject=subject, from_email=from_email, to=to, bcc=bcc,
                       connection=connection, headers=headers, cc=cc, reply_to=reply_to)
    alternative = MIMEMultipart('alternative')
    alternative.attach(MIMEText(body.encode('utf8'), 'plain', 'utf8'))
    alternative.attach(MIMEText(html_body.encode('utf8'), 'html', 'utf8'))
    related = MIMEMultipart('related')
    related.attach(alternative)
    for inline in html_inline_attachments:
        inline_attachment = msg._create_attachment(os.path.basename(inline), open(inline).read())
        inline_attachment.add_header('Content-Disposition', 'inline')
        inline_attachment.add_header('Content-ID', os.path.basename(inline))
        related.attach(inline_attachment)
    msg.attach(related)
    return msg

如果有人有一个更简单的解决方案,请让我知道: - )

If someone has a simpler solution, please let me know :-)

这篇关于使用内联图像和PDF附件创建HTML邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆