Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/cpython
Path: blob/main/Doc/includes/email-mime.py
12 views
1
# Import smtplib for the actual sending function.
2
import smtplib
3
4
# Here are the email package modules we'll need.
5
from email.message import EmailMessage
6
7
# Create the container email message.
8
msg = EmailMessage()
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'] = ', '.join(family)
14
msg.preamble = 'You will not see this in a MIME-aware mail reader.\n'
15
16
# Open the files in binary mode. You can also omit the subtype
17
# if you want MIMEImage to guess it.
18
for file in pngfiles:
19
with open(file, 'rb') as fp:
20
img_data = fp.read()
21
msg.add_attachment(img_data, maintype='image',
22
subtype='png')
23
24
# Send the email via our own SMTP server.
25
with smtplib.SMTP('localhost') as s:
26
s.send_message(msg)
27
28