Skip to content

Instantly share code, notes, and snippets.

@FBosler
Created December 4, 2021 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FBosler/465ac9970f908175e252fff40b32123f to your computer and use it in GitHub Desktop.
Save FBosler/465ac9970f908175e252fff40b32123f to your computer and use it in GitHub Desktop.
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