首页 > 解决方案 > 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"时,我收到错误消息,说它无法识别文件的类型。这可能有某种关系吗?

标签: pythonhtmlsvgmime-typesmimemultipart

解决方案


推荐阅读