1 # Import smtplib for the actual sending function 2 import smtplib 3 4 # Import the email modules we'll need 5 from email.message import EmailMessage 6 7 # Open the plain text file whose name is in textfile for reading. 8 with 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 15 msg['Subject'] = 'The contents of %s' % textfile 16 msg['From'] = me 17 msg['To'] = you 18 19 # Send the message via our own SMTP server. 20 s = smtplib.SMTP('localhost') 21 s.send_message(msg) 22 s.quit() 23