在iPhone,iPad上显示内联图像 [英] Displaying inline images on iPhone, iPad

查看:123
本文介绍了在iPhone,iPad上显示内联图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用内联图像在Django中创建一个电子邮件。

I'm trying to create an email in Django with inline images.

msg = EmailMultiAlternatives(...)
image_file = open('file_path', 'rb') 
img = MIMEImage(img_data)
image_file.close()
img.add_header('Content-ID', '<image1>') 
img.add_header('Content-Disposition', 'inline')
msg.attach(img)
msg.send()

在模板中,我将引用它:

And in the template I would reference it like so:

<img src="cid:image1" />

这在网络浏览器,外观,雷鸟...中都可以正常工作,除了苹果邮件客户端OSX,iPad和iPhone。图像显示两次。它们被正确放置,但它们也附在电子邮件的底部。我的问题是,如何摆脱底部的图像?或者我应该以不同的方式接收电子邮件中的图像。

This works fine in web browsers, outlook, thunderbird ... all except for the apple mail client on OSX, iPad and iPhone. The images are displayed twice. They are placed inline correctly but they are also attached to the bottom of the email. My question is, how do I get rid of the images at the bottom? or should I approach images in emails differently.

参考文献:

http ://djangosnippets.org/snippets/1507/

Django:如何发送带有嵌入图像的HTML电子邮件

推荐答案

不同的电子邮件客户端选择以不同的方式呈现 multipart / mixed 消息。

Different email clients choose to render multipart/mixed messages in different ways.

大多数客户端选择以内容的形式呈现每个部分(以multipart消息)的形式 - 按照添加到电子邮件中的顺序。 ,如果图像在 text / html 部分中引用,大多数客户端不再在作为内联所有部分过程的一部分。

Most clients choose to render each part (in a "multipart" message) inline – in the order they were added to the email. However, if an image is referred to in a text/html part, most clients don't display that image again later on as part of the "inlining all parts" process.

OSX和iOS上的Apple Mail是不同的,只要他们按照包含的顺序在 multipart / mixed 消息中显示每个部分,无论HTML和图像之间有什么内部引用。这将导致您的图像在HTML中显示一次,并在消息结束时再次自动进行内联。

Apple Mail on OSX and iOS are different, in so far as they will display each part in a multipart/mixed message in the order they were included, regardless of any inner references between HTML and images. This results in your images being displayed once within your HTML, and again at the end of the message where they've been inlined automatically.

解决方案是将HTML并将图像资产转换为单个相关的部分。即:

The solution is to group your HTML and image assets into a single related part. i.e.:

from django.core.mail import EmailMultiAlternatives
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# HTML + image container 
related = MIMEMultipart("related")

# Add the HTML
html = MIMEText('an image: <img src="cid:some_image"/>', "html")
related.attach(html)

# Add an image
with open("icon.png", "rb") as handle:
    image = MIMEImage(handle.read())
image.add_header("Content-ID", "<some_image>")
image.add_header("Content-Disposition", "inline")
related.attach(image)

# top level container, defines plain text version
email = EmailMultiAlternatives(subject="demo", body="plain text body",
                               from_email="foo@example.com",
                               to=["bar@example.com"])
# add the HTML version
email.attach(related)

# Indicate that only one of the two types (text vs html) should be rendered
email.mixed_subtype = "alternative"
email.send()

这篇关于在iPhone,iPad上显示内联图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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