Over a million developers have joined DZone.

Sending Html Mail With Embedded Image

·
See detail in this recipe.
The code below show the key parts of embedding an image.

# require the new email package
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage

...
...
msgRoot = MIMEMultipart('related')
...
...

# Assumes the image is in current directory
fp = open('test.jpg', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()

# Define the image's ID as referenced above
msgImage.add_header('Content-ID', '')
msgRoot.attach(msgImage)

...
smtp.sendmail(strFrom, strTo, msgRoot.as_string())
...
Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}