DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Sending Html Mail With Embedded Image

  • submit to reddit
        See detail in <a href=>this recipe</a>.
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(

# Define the image's ID as referenced above
msgImage.add_header('Content-ID', '<image1>')

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