• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Import smtplib for the actual sending function
2import smtplib
3
4# And imghdr to find the types of our images
5import imghdr
6
7# Here are the email package modules we'll need
8from email.message import EmailMessage
9
10# Create the container email message.
11msg = EmailMessage()
12msg['Subject'] = 'Our family reunion'
13# me == the sender's email address
14# family = the list of all recipients' email addresses
15msg['From'] = me
16msg['To'] = ', '.join(family)
17msg.preamble = 'You will not see this in a MIME-aware mail reader.\n'
18
19# Open the files in binary mode.  Use imghdr to figure out the
20# MIME subtype for each specific image.
21for file in pngfiles:
22    with open(file, 'rb') as fp:
23        img_data = fp.read()
24    msg.add_attachment(img_data, maintype='image',
25                                 subtype=imghdr.what(None, img_data))
26
27# Send the email via our own SMTP server.
28with smtplib.SMTP('localhost') as s:
29    s.send_message(msg)
30