email-simple.py 587 Bytes
Newer Older
1 2 3 4
# Import smtplib for the actual sending function
import smtplib

# 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 9
with open(textfile) as fp:
    # Create a text/plain message
10 11
    msg = EmailMessage()
    msg.set_content(fp.read())
12 13 14 15 16 17 18

# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'The contents of %s' % textfile
msg['From'] = me
msg['To'] = you

19
# Send the message via our own SMTP server.
20
s = smtplib.SMTP('localhost')
21
s.send_message(msg)
22
s.quit()