Created
December 4, 2021 20:10
-
-
Save FBosler/465ac9970f908175e252fff40b32123f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import smtplib, ssl | |
from dotenv import load_dotenv | |
load_dotenv() # take environment variables from .env. | |
PORT = 465 | |
def send_email(msg: str, recipient: str = None) -> None: | |
email = os.getenv("EMAIL") | |
password = os.getenv("EMAIL_PASSWORD") | |
smtp_server = os.getenv("SMTP_SERVER") | |
context = ssl.create_default_context() | |
with smtplib.SMTP_SSL(smtp_server, PORT, context=context) as server: | |
server.login(user=email, password=password) | |
SUBJECT = "Important Notification" | |
BODY = "\r\n".join((f"From: {email}", f"To: {email}", f"Subject: {SUBJECT}", "", msg)) | |
server.sendmail(email, [recipient or email], BODY) | |
if __name__ == "__main__": | |
send_email("This is a test") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment