I can only attach image files but embedded image files doesn't appears in the mail . It says The link image cannot be displayed file may be removed, deleted or renamed. verify link point to correct image file and location and also files attached are of same size. below is the code which I used
from requests_toolbelt import MultipartEncoder
import requests
import smtplib
import base64
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.image import MIMEImage
from os.path import basename
from jinja2 import Template
def send_mail(send_from: str, subject: str, text: str,send_to: list, files= None):
send_to= default_address if not send_to else send_to
main = Template('''
<html><body>
{% for image in pictures %}<img src="cid:{{image}}">{% endfor %}
</body></html>''')
msg = MIMEMultipart()
html = main.render(pictures=files)
part2 = MIMEText(html, 'html')
msg.attach(part2)
msg['From'] = send_from
msg['To'] = ', '.join(send_to)
msg['Subject'] = subject
for f in files or []:
with open(f, "rb") as fil:
msgImage = MIMEImage(fil.read())
ext = f.split('.')[-1:]
attachedfile = MIMEApplication(fil.read(), _subtype = ext)
fil.close()
msgImage.add_header('Content-ID', '<{}>'.format(f))
msgImage.add_header('content-Disposition','inline',filename=f)
msg.attach(msgImage)
attachedfile.add_header(
'content-disposition', 'attachment', filename=basename(f) )
msg.attach(msgImage)
msg.attach(attachedfile)
smtp = smtplib.SMTP(host="smtp-mail.outlook.com", port= 25)
smtp.starttls()
smtp.login(usr,pwd)
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.close()
send_mail(send_from= frommail,
subject="Daily backup Testing",
text='files added: ',
send_to= tomail,
files= files_list)
I get mail as this . Image path files are correct. when I print I get this
files ['check123\\Screenshot (161).png', 'check123\\Screenshot (163).png', 'check123\\Screenshot (164).png']
How can I solve this issue?