Never been to DZone Snippets before?

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

Sending html mail with embedded image (See related posts)

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', '<image1>')
msgRoot.attach(msgImage)

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

You need to create an account or log in to post comments to this site.


Click here to browse all 5140 code snippets

Related Posts