This is how you can send an email through python using smtp and ssl(Secure Sockets Layer, an encryption protocol)
1 min readAug 1, 2023
Following will be the code to send an email through python and I will be explaining every critical step in the comments indicated with #
import smtplib, ssl
port = 465 # For SSL
smtp_server = "smtp.gmail.com"
sender_email = "youremail@gmail.com" # Enter your address
receiver_email = "reciever@gmail.com" # Enter receiver address
password = "YourPassword" #this password will be the one you generate after
#you login with your account and then after enabling 2fa, you will go to app passwords
#and there you can generate an app password
message = """\
Subject: Hi there
This message is sent from Python.
"""
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message
#here the code is pretty self explanatory, we will, through the help of
#server.sendmail(), send a mail to the receiver