Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | # Import smtplib for the actual sending function |
| 2 | import smtplib |
| 3 | |
R David Murray | 29d1bc0 | 2016-09-07 21:15:59 -0400 | [diff] [blame] | 4 | # And imghdr to find the types of our images |
| 5 | import imghdr |
| 6 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 7 | # Here are the email package modules we'll need |
R David Murray | 29d1bc0 | 2016-09-07 21:15:59 -0400 | [diff] [blame] | 8 | from email.message import EmailMessage |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 9 | |
R David Murray | 29d1bc0 | 2016-09-07 21:15:59 -0400 | [diff] [blame] | 10 | # Create the container email message. |
| 11 | msg = EmailMessage() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 |
R David Murray | 29d1bc0 | 2016-09-07 21:15:59 -0400 | [diff] [blame] | 16 | msg['To'] = ', '.join(family) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | msg.preamble = 'Our family reunion' |
| 18 | |
R David Murray | 29d1bc0 | 2016-09-07 21:15:59 -0400 | [diff] [blame] | 19 | # Open the files in binary mode. Use imghdr to figure out the |
| 20 | # MIME subtype for each specific image. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 21 | for file in pngfiles: |
Berker Peksag | f9e3cf1 | 2015-02-25 18:14:09 +0200 | [diff] [blame] | 22 | with open(file, 'rb') as fp: |
R David Murray | 29d1bc0 | 2016-09-07 21:15:59 -0400 | [diff] [blame] | 23 | img_data = fp.read() |
| 24 | msg.add_attachment(img_data, maintype='image', |
| 25 | subtype=imghdr.what(None, img_data)) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 26 | |
| 27 | # Send the email via our own SMTP server. |
R David Murray | 29d1bc0 | 2016-09-07 21:15:59 -0400 | [diff] [blame] | 28 | with smtplib.SMTP('localhost') as s: |
| 29 | s.send_message(msg) |