python_examples/email_example.py

24 lines
524 B
Python
Raw Normal View History

2023-09-16 16:51:31 -04:00
"""email_example.py
----------------
2023-09-15 12:55:39 -04:00
This module sends an email with the contents of a specified text file as the
body. The "smtplib" and "email.message" modules are used to construct and send
the email.
"""
2023-08-23 11:11:51 -04:00
import smtplib
from email.message import EmailMessage
2023-08-23 12:02:52 -04:00
textfile = "textfile"
2023-08-23 11:11:51 -04:00
with open(textfile) as fp:
msg = EmailMessage()
msg.set_content(fp.read())
2023-08-23 12:02:52 -04:00
msg["Subject"] = f"The contents of {textfile}"
2023-09-15 12:55:39 -04:00
msg["From"] = "roland"
msg["To"] = "roland"
2023-08-23 11:11:51 -04:00
2023-08-23 12:02:52 -04:00
s = smtplib.SMTP("localhost")
2023-08-23 11:11:51 -04:00
s.send_message(msg)
s.quit()