I am trying to embed an image in an email. I have followed examples here, here and here and others however I cannot get the image to display.
import smtplib
import os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
logo = 'mylogo.png'
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = 'sender@email.com'
msg['To'] = 'recipient@email.com'
html = """\
<html>
<head></head>
<body>
<p>GREETING<br><br>
SOME TEXT<br>
MORE TEXT<br><br>
FAREWELL <br><br>
DISCLAIMER
</p>
<img src="cid:image1" alt="Logo" \>
</body>
</html> """
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html', 'utf-8')
msg.attach(part1)
msg.attach(part2)
fp = open(logo, 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', '<image1>')
msgImage.add_header('Content-Disposition', 'inline', filename=os.path.basename(logo))
msgImage.add_header("Content-Transfer-Encoding", "base64")
msg.attach(msgImage)
s = smtplib.SMTP(smtp_server,25)
s.sendmail(sender, recipient, msg.as_string())
s.quit()
When I execute this, I get an empty body with a red cross in it and no image. How do I get the image to be displayed inline with the email body?
I am using Outlook 2016. I know I can insert pictures when using Outlook itself and I have received 'normal' emails where others have inserted images within the text so surely this means I must be able to view images generated from a python script?
EDIT: I have looked at the solution given here, suggested as a possible duplicate, but this has not solved my problem either.
I have also tried sending the same email to a Gmail and a hotmail account and the same problem still arises so the problem is clearly something to do with code.