gpt4 book ai didi

ios - 在 iPhone、iPad 上显示内联图像

转载 作者:可可西里 更新时间:2023-11-01 03:08:20 25 4
gpt4 key购买 nike

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

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()

在模板中我会这样引用它:

<img src="cid:image1" />

这在 web 浏览器、outlook、thunderbird 中工作正常......除了 OSX、iPad 和 iPhone 上的苹果邮件客户端。图像显示两次。它们被正确地内联放置,但它们也附加在电子邮件的底部。我的问题是,如何摆脱底部的图像?或者我应该以不同的方式处理电子邮件中的图像。

引用资料:
http://djangosnippets.org/snippets/1507/
Django: How to send HTML emails with embedded images
creating a MIME email template with images to send with python / django

最佳答案

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

大多数客户选择以内联方式呈现每个部分(在“多部分”消息中)——按照它们被添加到电子邮件中的顺序。 但是,如果在 text/html 部分中引用了图像,大多数客户端以后不会再次显示该图像作为“内联所有部分”过程。

OSX 和 iOS 上的 Apple Mail 是不同的,因为它们按照包含它们的顺序在 multipart/mixed 消息中显示每个部分,而不管HTML 和图像之间的任何内部引用。这会导致您的图像在您的 HTML 中显示一次,然后在消息末尾自动内联显示。

解决方案是将您的 HTML 和图像 Assets 组合到一个相关部分。即:

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()

关于ios - 在 iPhone、iPad 上显示内联图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10103210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com