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