diff options
Diffstat (limited to 'Doc/includes/email-mime.py')
-rw-r--r-- | Doc/includes/email-mime.py | 34 |
1 files changed, 18 insertions, 16 deletions
diff --git a/Doc/includes/email-mime.py b/Doc/includes/email-mime.py index 6af2be0..7b1c028 100644 --- a/Doc/includes/email-mime.py +++ b/Doc/includes/email-mime.py @@ -1,29 +1,31 @@ # Import smtplib for the actual sending function import smtplib -# And imghdr to find the types of our images -import imghdr - # Here are the email package modules we'll need -from email.message import EmailMessage +from email.mime.image import MIMEImage +from email.mime.multipart import MIMEMultipart + +COMMASPACE = ', ' -# Create the container email message. -msg = EmailMessage() +# 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'] = ', '.join(family) -msg.preamble = 'You will not see this in a MIME-aware mail reader.\n' +msg['To'] = COMMASPACE.join(family) +msg.preamble = 'Our family reunion' -# Open the files in binary mode. Use imghdr to figure out the -# MIME subtype for each specific image. +# Assume we know that the image files are all in PNG format for file in pngfiles: - with open(file, 'rb') as fp: - img_data = fp.read() - msg.add_attachment(img_data, maintype='image', - subtype=imghdr.what(None, img_data)) + # Open the files in binary mode. Let the MIMEImage class automatically + # guess the specific image type. + fp = open(file, 'rb') + img = MIMEImage(fp.read()) + fp.close() + msg.attach(img) # Send the email via our own SMTP server. -with smtplib.SMTP('localhost') as s: - s.send_message(msg) +s = smtplib.SMTP('localhost') +s.sendmail(me, family, msg.as_string()) +s.quit() |