mirror of
https://github.com/python/cpython.git
synced 2024-12-01 03:01:36 +01:00
f9e3cf1f9f
Also, update email examples to use the context manager version of open(). Patch by Baptiste Mispelon.
31 lines
890 B
Python
31 lines
890 B
Python
# Import smtplib for the actual sending function
|
|
import smtplib
|
|
|
|
# Here are the email package modules we'll need
|
|
from email.mime.image import MIMEImage
|
|
from email.mime.multipart import MIMEMultipart
|
|
|
|
COMMASPACE = ', '
|
|
|
|
# Create the container (outer) email message.
|
|
msg = MIMEMultipart()
|
|
msg['Subject'] = 'Our family reunion'
|
|
# me == the sender's email address
|
|
# family = the list of all recipients' email addresses
|
|
msg['From'] = me
|
|
msg['To'] = COMMASPACE.join(family)
|
|
msg.preamble = 'Our family reunion'
|
|
|
|
# Assume we know that the image files are all in PNG format
|
|
for file in pngfiles:
|
|
# Open the files in binary mode. Let the MIMEImage class automatically
|
|
# guess the specific image type.
|
|
with open(file, 'rb') as fp:
|
|
img = MIMEImage(fp.read())
|
|
msg.attach(img)
|
|
|
|
# Send the email via our own SMTP server.
|
|
s = smtplib.SMTP('localhost')
|
|
s.send_message(msg)
|
|
s.quit()
|