I'm using the code below to build up a mail in html so to automatically send everyday. I have to include 7 images within the mail. Images are stored in .jpg (I've also tried .png) in the same folder where the script is.
The issue I have is with images not showing correctly: if I try to send the email to a gmail address I can see all of them properly, however whenever I send the email to a business email some images are visible and some other are not (I've tried emails from two different companies, using both Outlook and Web Outlook).
The images that are being shown change randomly, but it seems that the visible ones are always four. Does anyone have an idea about what could cause this and how I could overcome the issue? Many thanks!!
Here's my code
names = ['1.jpg','2.jpg','3.jpg','4.jpg','5.jpg','6.jpg','7.jpg']
msg = EmaiMessage()
msg['Subject'] = """AAA {d}-{m}-{y}""".format(d = day.day,m = day.month,y = day.year)
msg['From'] = "test@gmail.com"
msg['To'] = "test@gmail.com"
image_cid1 = make_msgid()
image_cid2 = make_msgid()
image_cid3 = make_msgid()
image_cid4 = make_msgid()
image_cid5 = make_msgid()
image_cid6 = make_msgid()
image_cid7 = make_msgid()
msg.add_alternative("""\
<html>
<body>
<p>Hi,<br><br>
Images for {d}-{m}-{y}.<br><br>
<hr>
</p>
<img src="cid:{image_cid1}" width="75%"><br><br><hr>
<img src="cid:{image_cid2}" width="75%"><br><br><hr>
<img src="cid:{image_cid3}" width="75%"><br><br><hr>
<img src="cid:{image_cid4}" width="75%"><br><br><hr>
<img src="cid:{image_cid5}" width="75%"><br><br><hr>
<img src="cid:{image_cid6}" width="75%"><br><br><hr>
<img src="cid:{image_cid7}" width="75%"><br><br><hr>
</body>
</html>
""".format(d = day.day, m = day.month, y = day.year,
image_cid1=image_cid1[1:-1],
image_cid2=image_cid2[1:-1],
image_cid3=image_cid3[1:-1],
image_cid4=image_cid4[1:-1],
image_cid5=image_cid5[1:-1],
image_cid6=image_cid6[1:-1],
image_cid7=image_cid7[1:-1]), subtype='html')
with open(names[0], 'rb') as img:
maintype, subtype = mimetypes.guess_type(img.name)[0].split('/')
msg.get_payload()[1].add_related(img.read(),
maintype=maintype,
subtype=subtype,
cid=image_cid1)
with open(names[1], 'rb') as img:
maintype, subtype = mimetypes.guess_type(img.name)[0].split('/')
msg.get_payload()[1].add_related(img.read(),
maintype=maintype,
subtype=subtype,
cid=image_cid2)
with open(names[2], 'rb') as img:
maintype, subtype = mimetypes.guess_type(img.name)[0].split('/')
msg.get_payload()[1].add_related(img.read(),
maintype=maintype,
subtype=subtype,
cid=image_cid3)
with open(names[3], 'rb') as img:
maintype, subtype = mimetypes.guess_type(img.name)[0].split('/')
msg.get_payload()[1].add_related(img.read(),
maintype=maintype,
subtype=subtype,
cid=image_cid4)
with open(names[4], 'rb') as img:
maintype, subtype = mimetypes.guess_type(img.name)[0].split('/')
msg.get_payload()[1].add_related(img.read(),
maintype=maintype,
subtype=subtype,
cid=image_cid5)
with open(names[5], 'rb') as img:
maintype, subtype = mimetypes.guess_type(img.name)[0].split('/')
msg.get_payload()[1].add_related(img.read(),
maintype=maintype,
subtype=subtype,
cid=image_cid6)
with open(names[6], 'rb') as img:
maintype, subtype = mimetypes.guess_type(img.name)[0].split('/')
msg.get_payload()[1].add_related(img.read(),
maintype=maintype,
subtype=subtype,
cid=image_cid7)
try:
smtp = smtplib.SMTP("smtp.gmail.com", 587, timeout=600)
smtp.starttls()
smtp.login('test', 'testpwd')
smtp.sendmail(fromaddr, toaddr, msg.as_string())
smtp.quit()
except:
print("Unexpected error while sending email:", sys.exc_info()[0])
raise
sys.exit(1)