Skip to content

Instantly share code, notes, and snippets.

@ndamulelonemakh
Last active November 25, 2023 05:00
Show Gist options
  • Select an option

  • Save ndamulelonemakh/d2a3ee0708fe24ede7b0f5e2b6686b70 to your computer and use it in GitHub Desktop.

Select an option

Save ndamulelonemakh/d2a3ee0708fe24ede7b0f5e2b6686b70 to your computer and use it in GitHub Desktop.
Automatically Send an email using Python's smtplib
import smtplib
import ssl
from smtplib import SMTPAuthenticationError
# Simple Python snippet to automatically send emails using gmail
# Docs: https://docs.python.org/3.10/library/smtplib.html
DEFAULT_SSL_PORT = 465
GMAIL_SERVER_ADDRESS = "smtp.gmail.com"
DEFAULT_SENDER_EMAIL_ADDRESS = "<sender-address>"
password = input("Sender password: ")
email_body = "Hello world"
# Create a secure SSL context
context = ssl.create_default_context()
with smtplib.SMTP_SSL(GMAIL_SERVER_ADDRESS, DEFAULT_SSL_PORT, context=context) as server:
exception_instance = server.login(DEFAULT_SENDER_EMAIL_ADDRESS, password)
if exception_instance is not None: # type: SMTPAuthenticationError
print(exception_instance)
raise exception_instance
server.sendmail(from_addr=DEFAULT_SENDER_EMAIL_ADDRESS, to_addrs="<recipients>", msg=email_body)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment