blob: c610242f11f843f5c5916628fa55142a929262cc [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001# Import smtplib for the actual sending function
2import smtplib
3
R David Murray29d1bc02016-09-07 21:15:59 -04004# And imghdr to find the types of our images
5import imghdr
6
Georg Brandl116aa622007-08-15 14:28:22 +00007# Here are the email package modules we'll need
R David Murray29d1bc02016-09-07 21:15:59 -04008from email.message import EmailMessage
Georg Brandl116aa622007-08-15 14:28:22 +00009
R David Murray29d1bc02016-09-07 21:15:59 -040010# Create the container email message.
11msg = EmailMessage()
Georg Brandl116aa622007-08-15 14:28:22 +000012msg['Subject'] = 'Our family reunion'
13# me == the sender's email address
14# family = the list of all recipients' email addresses
15msg['From'] = me
R David Murray29d1bc02016-09-07 21:15:59 -040016msg['To'] = ', '.join(family)
Georg Brandl116aa622007-08-15 14:28:22 +000017msg.preamble = 'Our family reunion'
18
R David Murray29d1bc02016-09-07 21:15:59 -040019# Open the files in binary mode. Use imghdr to figure out the
20# MIME subtype for each specific image.
Georg Brandl116aa622007-08-15 14:28:22 +000021for file in pngfiles:
Berker Peksagf9e3cf12015-02-25 18:14:09 +020022 with open(file, 'rb') as fp:
R David Murray29d1bc02016-09-07 21:15:59 -040023 img_data = fp.read()
24 msg.add_attachment(img_data, maintype='image',
25 subtype=imghdr.what(None, img_data))
Georg Brandl116aa622007-08-15 14:28:22 +000026
27# Send the email via our own SMTP server.
R David Murray29d1bc02016-09-07 21:15:59 -040028with smtplib.SMTP('localhost') as s:
29 s.send_message(msg)