python-scripts/sendmail.py

33 lines
818 B
Python

#!/usr/bin/env python3
import smtplib
import ssl
smtp_server = "mail.example.com"
port = 465 # For SSL
sender_email = "user01@example.com"
password = "smtpPassw0rd"
# password = input("Type your password and press enter: ") # Interactive
receiver_email = "user02@example.com"
message = """\
From: user01@example.com
Subject: Test Email
This message is sent from Python."""
# Create a secure SSL context
context = ssl.create_default_context()
# Try to log in to server and send email
try:
server = smtplib.SMTP_SSL(smtp_server, port, context=context)
server.ehlo() # Can be omitted
server.login(sender_email, password)
# Send email
server.sendmail(sender_email, receiver_email, message)
except Exception as e:
# Print any error messages to stdout
print(e)
finally:
server.quit()