How to send an email in Python Language?

To send an email, Python provides smtplib and email modules. Import these modules into the created mail script and send mail by authenticating a user.

It has a method SMTP(smtp-server, port). It requires two parameters to establish SMTP connection.

A simple example to send an email is given below.

import smtplib
# Calling SMTP
s = smtplib.SMTP(‘smtp.gmail.com’, 587)
# TLS for network security
s.starttls()
# User email Authentication
s.login(“sender_email_id”, “sender_email_id_password”)
# message to be sent
message = “Message_you_need_to_send”
# sending the mail
s.sendmail(“sender_email_id”, “receiver_email_id”, message)
It will send an email to the receiver after authenticating sender username and password.