0

Getting a red cross while trying to embed a png file created through pyplot in email The code I am using is here below. What can I add in the current html to get rid of this

I need to add the image in the email body

fig, ax = plt.subplots(figsize=(10,6))
ax.plot(
         query['dt'],
         query['cnt'],
         marker = 'o',
         markerfacecolor = 'DeepSkyBlue',
         linewidth = 3,
         color = 'MediumSlateBlue' )
ax.set(xlabel = 'D A T E', ylabel = 'C O U N T')

plt.setp(ax.get_xticklabels(), rotation = 90)


plt.savefig("C:/Users/quratulain.zulfiqar/Desktop/f.png", bbox_inches = "tight")




body = """<html>
    <head>
    <style>
       #customers {
  font-family: "proxima-nova", sans-serif;
  border-collapse: collapse;
  width: 100%;
}


#customers td, #customers th {
  border: 1px solid #CB3D57;
  padding: 8px;
}

#customers tr:nth-child(even){background-color: #CB3D57;}

#customers tr:hover {background-color: #CB3D57;}

#customers th {
  padding-top: 12px;
  padding-bottom: 10px;
  text-align: center;
  background-color: #CB3D57;
  color: red;
} 
    </style>
    </head>
    
      <body>
<span style="text-align:center;"> <u><h3>"""+"AGENT MAPPING COUNT"+"""</h3></u> </span>

<div><img src= "cid:C:/Users/quratulain.zulfiqar/Desktop/f.png" ></img></div>
   <p><p>
<div style='vertical-align:middle; display:att;'>
   <p><p>
   <p><p>
   <p>Regards,<p>
   </body>
    </html>  
"""


# In[288]:


msg1 = MIMEMultipart('alternative')
msg1['Subject'] = email_subject
msg1["From"]=from_label  
msg1["To"]=to_label 
msg1["CC"]=CC 
file_list=[]

for fl in file_list:
    with open(fl, "rb") as fil:
        part = MIMEApplication(fil.read(),Name=basename(fl))
    part['Content-Disposition'] = 'attachment; filename="%s"' % basename(fl)
    msg1.attach(part)
part1 = MIMEText(body, 'html')
msg1.attach(part1)

with open('C:/Users/quratulain.zulfiqar/Desktop/f.png', 'rb') as f:
    # set attachment mime and file name, the image type is png
    mime = MIMEBase('image','png', filename='f.png')
    # add required header data:
    mime.add_header('Content-Disposition', 'attachment', filename='f.png')
    mime.add_header('X-Attachment-Id', '0')
    mime.add_header('Content-ID', '<0>')
    mime.set_payload(f.read())
    encode_base64(mime)

    msg1.attach(mime)
server = smtplib.SMTP(email_server, email_port)
server.sendmail(from_email,to_emails, msg1.as_string())
server.quit()

I know there are other ways but since we are already this format which we were following so I need to stick to that and update it here.

2 Answers2

0

The "cid:" string that you send in the email has to exactly match the Content-ID: header in the image attachment (well, the Content-ID gets angle brackets). That's how the email app makes the connection. You are specifying a path on YOUR computer for the cid, which is going to mean absolutely nothing when it is rendered on someone else's computer.

embedding image in html email

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
0

Changing the img src to the attached image name worked as @tim mentioned

<div><img src= "cid:f.png"></img></div>