python - MIMEImage 中包含的 svg 文件未正确显示在 html 电子邮件中
问题描述
我需要发送带有 svg 徽标文件的电子邮件。我附上了它,但它没有正确显示。
这是我的代码:
msg=MIMEMultipart()
img_msg=MIMEMultipart('related')
full_path_to_logo_file=os.path.join('C:\\Users\\emails\\images', 'email-logo.svg')
with open(full_path_to_logo_file, 'rb') as logo_file:
logo_in_binary=logo_file.read()
logo_image=MIMEImage(logo_in_binary, _subtype="svg")
logo_image.add_header('Content-ID','<logo_image_svg>')
img_msg.attach(logo_image)
full_path_to_background_image_file=os.path.join('C:\\Users\\emails\\images', 'email-background.jpg')
with open(full_path_to_background_image_file, 'rb') as background_image_file:
background_in_binary=background_image_file.read()
background_image=MIMEImage(background_in_binary, _subtype="jpg")
background_image.add_header('Content-ID','<background_image_jpg>')
img_msg.attach(background_image)
msg.attach(img_msg)
带有jpg图像的部分工作正常。
这是它在电子邮件中的外观。
svg文件附加到电子邮件中,当我用编辑器打开它时,我看到了 svg-xml 路径。因此,看起来它在生成的数据方面工作,但不知何故它没有正确附加到电子邮件。
这是电子邮件的 html/css 代码:
<!--[if mso]>
<table cellspacing="0" cellpadding="0" border="0" width="350" align="center">
<tr>
<td>
<![endif]-->
<table cellspacing="0" cellpadding="0" border="0" width="100%" style="border-collapse: collapse; margin: 0 auto;">
<tr>
<td bgcolor="#000000" style="background-color: #000000; padding: 0 25px;">
<table cellspacing="0" cellpadding="0" border="0" width="100%" style="border-collapse: collapse;">
<tr>
<td style="padding: 10px 0; text-align: center; font-family: 'Roboto', sans-serif; color: #FFFFFF;">
<img src="cid:logo_image_svg" width="90" height="50" alt=" " border="0" style="height: auto; background: #000000; font-family: 'Roboto', sans-serif; font-size: 10px; line-height: 10px; color: #FFFFFF;">
</td>
</tr>
</table>
</td>
</tr>
</table>
<!--[if mso]>
</td>
</tr>
</table>
<![endif]-->
</div>
PS我已经把这个发送到gmail帐户了。
PPS 另一个有趣的事情是,当我删除_subtype="svg"时,我收到错误消息,说它无法识别文件的类型。这可能有某种关系吗?
解决方案
推荐阅读
- c# - 如何使用具有某些 Id 的一列加入或搜索 Id
- android - 尝试更新到 Kotlin 1.4.0 时出现奇怪的错误。如何使其与 Gradle 和 IntelliJ IDEA 2020.2.1 一起使用?
- ios - NSUbiquitousKeyValueStore 大部分时间不保存数据
- c++ - 将 std::span 分配给 std::vector 的最简单方法
- r - 使用列名在小标题中更改值
- javascript - CSS 中的 SVG 动画
- python - Python pandas 中的灵活链接
- docker - 无法在 kubernetes 命名空间下启动 pod
- mysql - 同一列上的索引和分区
- python - 在 Python 中为每个 Celery 任务分配特定数量的工人