Home | History | Annotate | Download | only in includes
      1 # Import smtplib for the actual sending function
      2 import smtplib
      3 
      4 # And imghdr to find the types of our images
      5 import imghdr
      6 
      7 # Here are the email package modules we'll need
      8 from email.message import EmailMessage
      9 
     10 # Create the container email message.
     11 msg = EmailMessage()
     12 msg['Subject'] = 'Our family reunion'
     13 # me == the sender's email address
     14 # family = the list of all recipients' email addresses
     15 msg['From'] = me
     16 msg['To'] = ', '.join(family)
     17 msg.preamble = 'Our family reunion'
     18 
     19 # Open the files in binary mode.  Use imghdr to figure out the
     20 # MIME subtype for each specific image.
     21 for 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.
     28 with smtplib.SMTP('localhost') as s:
     29     s.send_message(msg)
     30