Search code examples
pythonmatplotliboutlookseabornpywin32

Problem with embed Python plot in email showing blank


I'm trying to send an email with embedded plots with Python. I can't use "cid" to embed the plots due to the fact that some recipients won't be able to see them that way.

I tried to embed the plot via setting the property by SchemaName, but when I send the email the plot show as blank.

My code is the following:

import win32com.client as win32
import seaborn as sns
import matplotlib.pyplot as plt

sns.lineplot(x='Date', y='Total',hue = "Company", 
             data=df1)
plt.savefig('C:/MyDocs/Plot1.jpg')

sns.lineplot(x='Date', y='Total',hue = "City", 
             data=df2)
plt.savefig('C:/MyDocs/Plot2.jpg')

sns.lineplot(x='Date', y='Total',hue = "Name", 
             data=df3)
plt.savefig('C:/MyDocs/Plot3.jpg')

plot1 = 'C:/MyDocs/Plot1.jpg'
plot2 = 'C:/MyDocs/Plot2.jpg'
plot3 = 'C:/MyDocs/Plot3.jpg'

outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = myemail
mail.Subject = Testing
attachment = mail.Attachments.Add(plot1, 0x5, 0, "display name1")
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "MyId")
attachment = mail.Attachments.Add(plot2, 0x5, 0, "display name2")
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "MyId2")
attachment = mail.Attachments.Add(plot3, 0x5, 0, "display name3")
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "MyId3")

html= ""</html>
<p class=MsoNormal><span lang=ES style='mso-ansi-language:ES'>Hello,<o:p></o:p></span></p>
Testing plots
<p class=MsoNormal><span lang=ES style='mso-ansi-language:ES'><o:p>&nbsp;</o:p></span></p>
<v:imagedata src='cid:"MyId"'/>
</v:shapetype><v:shape id="Imagen_x0020_1" o:spid="_x0000_i1025" type="#_x0000_t75"
 alt="" style='width:482.25pt;height:321.75pt'>
<img width=643 height=429
src='cid:"MyId"' style='height:4.468in;width:6.697in'
v:shapes="Imagen_x0020_1"><![endif]></p>
<v:imagedata src='cid:"MyId2"'/>
</v:shapetype><v:shape id="Imagen_x0020_1" o:spid="_x0000_i1025" type="#_x0000_t75"
 alt="" style='width:482.25pt;height:321.75pt'>
<img width=643 height=429
src='cid:"MyId2"' style='height:4.468in;width:6.697in'
v:shapes="Imagen_x0020_1"><![endif]></p>
<v:imagedata src='cid:"MyId3"'/>
</v:shapetype><v:shape id="Imagen_x0020_1" o:spid="_x0000_i1025" type="#_x0000_t75"
 alt="" style='width:482.25pt;height:321.75pt'>
<img width=643 height=429
src='cid:"MyId3"' style='height:4.468in;width:6.697in'
v:shapes="Imagen_x0020_1"><![endif]></p>
</body>

</html>

mail.HTMLBody = html
mail.Send()
"""

But when I receive the email, the outline of the plots are there, but the plots show as blank. I've tried using base64 encoding but the same happens

Thanks!


Solution

  • Your signature images use vml conditionals - you need to force the standard img tags instead of shape\imagedata. Replace <!--[if gte vml 1]> with <!--[if gte vml 9]> and <![if !vml]> with <![if !vmlxyz]> to disable the vml conditionals. It is crude, but it works.