2685
python - Sending Multipart html emails which contain embedded images - Stack Overflow
# Send an HTML email with an embedded image and a plain text message for
# email clients that don't want to display the HTML.

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage

# Define these once; use them twice!
strFrom = 'from@example.com'
strTo = 'to@example.com'

# Create the root message and fill in the from, to, and subject headers
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'test message'
msgRoot['From'] = strFrom
msgRoot['To'] = strTo
msgRoot.preamble = 'This is a multi-part message in MIME format.'

# Encapsulate the plain and HTML versions of the message body in an
# 'alternative' part, so message agents can decide which they want to display.
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)

msgText = MIMEText('This is the alternative plain text message.')
msgAlternative.attach(msgText)

# We reference the image in the IMG SRC attribute by the ID we give it below
msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>Nifty!', 'html')
msgAlternative.attach(msgText)

# This example assumes the image is in the 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)

# Send the email (this example assumes SMTP authentication is required)
import smtplib
smtp = smtplib.SMTP()
smtp.connect('smtp.example.com')
smtp.login('exampleuser', 'examplepass')
smtp.sendmail(strFrom, strTo, msgRoot.as_string())
smtp.quit()
python  email  images  smtp 
5 days ago
« earlier      
accounting addin ajax algorithm algorithms alternative analysis analytics api arch article articles asm assembly backup banking bash batch blog book books browser build business c c++ charts cheatsheet chrome cli clojure cmd code color colors colorscheme command-line commands comparison compiler config configuration console conversion css cygwin data data-analysis data-science database date dates db debug debugging deployment design development dll docs documentation ebook ebooks economics editor education electricity emacs email energy entrepreneurship ep errors example examples excel extension file files filesystem finance firefox font fonts forms formulas framework free french function functional functions gdb git gmail google government graph graphics graphing graphs great-plains grid gui guide hacks haskell help howto html http icons images inkscape inspiration interesting interpreter investing java javascript jquery js json keyboard keys language layout learning library lightweight linux lisp list lua machine-learning macros make maps markdown marketing math matplotlib memory minimal modeling music mutt network networking nginx notes ocaml online open-source openoffice opensource optimization options os outlook parser parsing pdf performance philadelphia phone plugin poker postgres postgresql pricing probability programming python r radio reference regex scheme screen script scripting search secure security server setup shell shortcut-keys shortcuts small smtp software spreadsheet spreadsheets sql sqlite sqlserver ssh startup statistics stats string syntax sysadmin tables templates terminal testing text tips todo tool tools toread tree tricks tutorial tutorials typography ubuntu ui unix utilities utility vba video videos vim vimscript virtual_machine visualization vm web webdesign wiki windows words wsgi

Copy this bookmark:



description:


tags: