Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Sending Html Mail With Embedded Image

DZone's Guide to

Sending Html Mail With Embedded Image

·
Free Resource
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:

Opinions expressed by DZone contributors are their own.

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 }}