1# Import smtplib for the actual sending function
2import smtplib
3
4# Import the email modules we'll need
5from email.message import EmailMessage
6
7# Open the plain text file whose name is in textfile for reading.
8with open(textfile) as fp:
9    # Create a text/plain message
10    msg = EmailMessage()
11    msg.set_content(fp.read())
12
13# me == the sender's email address
14# you == the recipient's email address
15msg['Subject'] = 'The contents of %s' % textfile
16msg['From'] = me
17msg['To'] = you
18
19# Send the message via our own SMTP server.
20s = smtplib.SMTP('localhost')
21s.send_message(msg)
22s.quit()
23