Skip to content

Instantly share code, notes, and snippets.

@jgs03177
Created April 8, 2024 13:43
Show Gist options
  • Save jgs03177/f25a15090d1c9e4735f8368150ae1688 to your computer and use it in GitHub Desktop.
Save jgs03177/f25a15090d1c9e4735f8368150ae1688 to your computer and use it in GitHub Desktop.
python email example
# Import smtplib for the actual sending function
import smtplib
import getpass
# Import the email modules we'll need
from email.message import EmailMessage
# Create a text/plain message
msg = EmailMessage()
msg.set_content('Testing email notification with python!')
smtp_server = "mail.example.com"
smtp_port = 587
mail_id = input("ID: ")
# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = f'Python email notification'
msg['From'] = mail_id
msg['To'] = mail_id
# Send the message via gmail (login)
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
mail_pw = getpass.getpass() # get password. may be unsafe, but convenient
server.login(mail_id, mail_pw)
del mail_pw
# send message
server.send_message(msg)
response, msg = server.quit()
if response==221:
print("mail sent successfully.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment