Skip to content

Instantly share code, notes, and snippets.

@raghavmri
Last active March 16, 2022 15:45
Show Gist options
  • Save raghavmri/911ef01fd61b946b337c8b795a07aaea to your computer and use it in GitHub Desktop.
Save raghavmri/911ef01fd61b946b337c8b795a07aaea to your computer and use it in GitHub Desktop.
A simple way of sending emails using python
# !/usr/bin/python
import smtplib
sender = 'sender@fromdomain.com'
receivers = ['receiver@todomain.com']
# Your Email Meta Data and your message
message = """
From: From Person <sender@fromdomain.com>
To: To Person <receiver@todomain.com>
Subject: SMTP e-mail test
This is a test e-mail message.
"""
try:
# use SMTP_SSL() for secure connections
smtpObj = smtplib.SMTP(
host="smtp.gmail.com", # The host of your SMTP server
port=25, # The port of your SMTP server(usually 25, 465, 587)
timeout=10 # The timeout in seconds
) # Creates a SMTP object
smtpObj.login(
user="admin", # Your username
password="this_is_a_secure_password" # Your password
) # Logins into your SMTP server
smtpObj.sendmail(sender, receivers, message) # Sends the email
print("Successfully sent email")
except smtplib.SMTPException:
print("Error: unable to send email")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment