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