# How to send emails with python using SMTP
# import SMTP library into your project using the command below
import smtplib
# Assuming you created two new Gmail and yahoo email accounts, create a connection to the Gmail email server by using the code below.
connection = smtplib.SMTP("smtp.gmail.com")
# Create variables to hold your email credentials
my_email = "sampleemail@gmail.com"
my_password = "passwaord"
# Secure your connection.
# It's important that you secure your connection to the Gmail mail servers.
# Securing your connection to the servers will prevent unauthorized access to your email in the event it's intercepted in transit.
# This is done by calling the tls function on your connection.
connection.starttls()
# The tls function is an inbuilt method that encrypts your email data sent via the connection established to email servers.
# The method is drawn from transport layer security protocol designed to provide communications security over a network.
#log in to your email account
# Run the code below to log in. The log-in method takes two parameters to facilitate a successful log-in: your email and password.
# Make sure there are no typos on the email and password held on the variables we created earlier.
connection.login(user=my_email, password=my_password)
# Sending the email.
# We are going to call the sendmail method on our connection. The method takes up 3 parameters :
# The sending address.
# The recipient's address. ( Avoid typos while typing it out to avoid errors.)
# The message with its subject and its body. Run the following command.
connection.sendmail(from_addr=my_email, to_addrs="receipient_email@yahoo.com", msg=" Subject: My_subject
My message body"
# Note: The subject and the body are separated using back slashes with an n to create new lines between them.
# Close the connction.
# Close the connection to the Gmail mail servers.
connection.close()