0

I have been trying to embed images to an email using MIMEImage but even after replicating examples I find online, I keep having the same issue...

Here is the part of the HTML that embeds the image:

<img width=779 height=467 style='width:8.1145in;height:4.8645in' src="cid:image001" alt="HRX Trend"> 

This is my Python code:

msg = MIMEMultipart('alternative')
msg['Subject'] = "test for daily email"
msg['From'] = me
msg['To'] = you

fp = open('path\\name.jpeg', 'rb')
msgImage = MIMEImage(fp.read())

msgImage.add_header('Content-ID', '<image001>')

part2 = MIMEText(html, 'html')

msg.attach(part2)
msg.attach(msgImage)

s = smtplib.SMTP('domain', port)
s.sendmail(me, you, msg.as_string())
s.quit()

But, when the email sends, I keep getting the error saying: The linked image cannot be displayed. The file may have been moved, renamed, or deleted.

Screenshot of what the email ends up looking like:

Screenshot of what the email ends up looking like

I have no idea what I am doing wrong...

Thank you!

Abe
  • 11
  • 6
  • For your case, this link will helps you to resolve the issue. https://stackoverflow.com/questions/7755501/embed-picture-in-email – y051 Jul 13 '22 at 17:12
  • Thank you! I changed `msg = MIMEMultipart('alternative')` to `msg = MIMEMultipart()` and it is working now! – Abe Jul 13 '22 at 19:06

1 Answers1

0

I updated the message container and the issue was resolved:

Original:

msg = MIMEMultipart('alternative')
msg['Subject'] = "test for daily email"
msg['From'] = me
msg['To'] = you

New:

msg = MIMEMultipart()
msg['Subject'] = "test for daily email"
msg['From'] = me
msg['To'] = you
Abe
  • 11
  • 6