I have checked many posts about it, but still cant find a solution. I am able to send email with embedded pictures, but the email also includes those pictures as attachments, and I need only embedded pictures. I have tried many variations, with 'related' type, 'mixed'. Also with html code inside Python program (not in Jinja2 template), but i cant make it work.
list_of_images = get_graphs() #list with file names
# here if I put "related" - images are sent ONLY as attachments
mail = MIMEMultipart()
for filename in list_of_images:
fp = open(filename, 'rb')
msg_img = MIMEImage(fp.read())
fp.close()
msg_img.add_header('Content-ID', '<{}>'.format(filename))
msg_img.add_header('Content-Disposition', 'inline', filename=filename)
mail.attach(msg_img)
#Jinja2 for html template
env = Environment(loader=FileSystemLoader('.'))
main = env.get_template('images.tpl')
html = main.render(pictures=list_of_images)
msgHtml = MIMEText(html, 'html')
mail.attach(msgHtml)
mail['Subject'] = "TEST"
mail['From'] = "email@addr"
mail['To'] = "email@addr"
s = smtplib.SMTP("localhost")
s.sendmail(mail['From'], "email@addr", mail.as_string())
s.quit()
jinja template:
<html>
<body>
{% for image in pictures %}
<img src="cid:{{image}}">
{% endfor %}
</body>
</html>