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:
JavaScript
x
2
1
<img width=779 height=467 style='width:8.1145in;height:4.8645in' src="cid:image001" alt="HRX Trend">
2
This is my Python code:
JavaScript
1
19
19
1
msg = MIMEMultipart('alternative')
2
msg['Subject'] = "test for daily email"
3
msg['From'] = me
4
msg['To'] = you
5
6
fp = open('path\name.jpeg', 'rb')
7
msgImage = MIMEImage(fp.read())
8
9
msgImage.add_header('Content-ID', '<image001>')
10
11
part2 = MIMEText(html, 'html')
12
13
msg.attach(part2)
14
msg.attach(msgImage)
15
16
s = smtplib.SMTP('domain', port)
17
s.sendmail(me, you, msg.as_string())
18
s.quit()
19
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:
I have no idea what I am doing wrong…
Thank you!
Advertisement
Answer
I updated the message container and the issue was resolved:
Original:
JavaScript
1
5
1
msg = MIMEMultipart('alternative')
2
msg['Subject'] = "test for daily email"
3
msg['From'] = me
4
msg['To'] = you
5
New:
JavaScript
1
5
1
msg = MIMEMultipart()
2
msg['Subject'] = "test for daily email"
3
msg['From'] = me
4
msg['To'] = you
5