/Doc/includes/email-mime.py

http://unladen-swallow.googlecode.com/ · Python · 31 lines · 17 code · 5 blank · 9 comment · 1 complexity · 11ea1de7556f06c17966a16c3fb2fb24 MD5 · raw file

  1. # Import smtplib for the actual sending function
  2. import smtplib
  3. # Here are the email package modules we'll need
  4. from email.mime.image import MIMEImage
  5. from email.mime.multipart import MIMEMultipart
  6. COMMASPACE = ', '
  7. # Create the container (outer) email message.
  8. msg = MIMEMultipart()
  9. msg['Subject'] = 'Our family reunion'
  10. # me == the sender's email address
  11. # family = the list of all recipients' email addresses
  12. msg['From'] = me
  13. msg['To'] = COMMASPACE.join(family)
  14. msg.preamble = 'Our family reunion'
  15. # Assume we know that the image files are all in PNG format
  16. for file in pngfiles:
  17. # Open the files in binary mode. Let the MIMEImage class automatically
  18. # guess the specific image type.
  19. fp = open(file, 'rb')
  20. img = MIMEImage(fp.read())
  21. fp.close()
  22. msg.attach(img)
  23. # Send the email via our own SMTP server.
  24. s = smtplib.SMTP()
  25. s.sendmail(me, family, msg.as_string())
  26. s.quit()