Sending a email in python

Yash Maheshwari
6 min readJun 17, 2020

What Does this Program do?

This program will ask the user who to send the email to and then ask them the topic, how many lines, and the message. Then it will send the email to the person the user said to.

Steps to Sending a email:

On a high level, below are the list of steps to send a email to someone:

i. Setting variables like who is it from and who is it too.

ii. Get the subject and message

iii. send the email

Step #1: Install Packages

These instructions are meant for python 3.8, if you have an older version you can upgrade or try to follow along, but the code won’t be exactly the same.

Before you start writing python codes, let’s install some packages.

Go to the terminal and type “pip install” with the package name. An example is, “pip install smtplib” and press “enter” key.

Similarly, install smtplib in the terminal

If any of these packages have already been installed on your computer, you will get the below message “Requirement already satisfied”:

If the package installation works, it will look like something like the below image “Successfully installed”:

If there is an error, there will be a red message like the below image “ERROR: No matching distribution”. Fix the error using the correct package name and successfully install all these packages.

Once all the above packages are successfully installed, it’s time to move on to importing the packages in the program and start coding.

Step #2: Allow Unsecure Apps

In this step you will go to the link below and scroll down until you find a section that says Less secure app access. You will turn that On, so python can access you email. This is not recommended for your main account, you should make a side account and use that one. You have to do this on the account you will be using to send the email with.

https://myaccount.google.com/security?gar=1

Step #3: Import Packages

In this step you will import smtplib so you can send emails in your program

import smtplib

Step #4: Create the Variables

In this step you will create a variable called EMAIL_ADDRESS and a variable called PASSWORD and you will ask the user what you want in each of those variables. It is important that the email address and the password are actual accounts.

EMAIL_ADDRESS = input("What is your email address")
PASSWORD = input("What is your password")

Step #5: Who Should I Send To?

In this step you will ask the user who to send the email to, by using a command called input.

TO = input("Who should I send the email too?: ")

Step #6: Defining Function

In this step you will define a function called send_email and have it take two parameters, subject and msg(message).

def send_email(subject, msg):

Step #7: Try and Except

In this step, you will add a try and except block to the code. A try and except block it just in case there is an error, it won’t crash the program, it just will do another set of instructions.

try:except:

Step #7: Making the server

In this step, you will add a server in the try block. To add a server you create a variable called server and set smtplib.SMTP to it.

server = smtplib.SMTP('smtp.gmail.com:587')

Step #8: Supporting Extensions

In this step, you will add a command to the server called ehlo. Ehlo is used when sending emails, because it identifies itself when it is connecting to another email server. It tells the receiving server it supports extensions compatible with ESMTP.

server.ehlo()

Step #9: Encrypting the Email

In this step, you will use StartTLS. StartTLS is used to encrypt the message, so it is more secure.

server.starttls()

Step #10: Login

In this step, you will login to your account, so you can send the email from your account.

server.login(EMAIL_ADDRESS, PASSWORD)

Step #11: Sending the message

In this step, you will send the message to who you set “TO” to.

server.sendmail(EMAIL_ADDRESS, TO, message)

Step #12: Quit the server

In this step, you will quit the server, so there is no more access to your account.

server.quit()

Step #13: Output

In this step, you will create a variable text and set it to, “The email was sent”. You do this so the user knows the email was sent.

text = "The email was sent!"

Step #14: Error Output

In this step, you will go to the except block in your code and create a variable text. You will set it to, “The email failed to send”. You do this so the user knows the email was not sent, if there was a error.

text = "The email failed to send!"

Step #15: return

In this step, you will go after the except block and return the text, so the user program knows if the email was sent, or if it was not sent.

return text

Step #16: Subject and Lines

In this step, you will go out of the function you just created called send_email and ask the user for 2 variables, subject and lines. You make the lines a number by having the int keyword and parentheses.

subject = input("Enter the subject: ")

lines = int(input("How many lines are there?: "))

Step #17: Variable called final

In this step, you will add a variable called final and set it to an empty string.

final = ""

Step #18: For Loop

In this step, you will add a for loop that runs the number of times that the user wants which is defined in the variable “lines”.

for i in range(lines):

Step #19: Asking for the message and adding it to final

In this step, you will ask the user for the message that they want in the line and add that to final. Then you will add a new line to final, so all the lines are on their own line, not in one big line.

msg = input("Enter a line: ")

final += msg
final += "\n"

Step #20: Calling the function and printing output

In this step, you will call the function we created called send_email and input the subject and message in it. The subject is the variable subject and the message is the variable final

text = send_email(subject, final)
print(text)

Below is the full source code

import smtplibEMAIL_ADDRESS = input("What is your email address")
PASSWORD = input("What is your password")
TO = input("Who is the email too?: ")

def send_email(subject, msg):
try:
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(EMAIL_ADDRESS, PASSWORD)
message = 'Subject: {}\n\n{}'.format(subject, msg)
server.sendmail(EMAIL_ADDRESS, TO, message)
server.quit()
text = "The email was sent!"
except:
text = "The email failed to send!"
return text

subject = input("Enter the subject: ")

lines = int(input("How many lines are there?: "))

final = ""

for i in range(lines):
msg = input("Enter a line: ")

final += msg
final += "\n"

text = send_email(subject, final)
print(text)

Thanks for staying till the end. If you enjoyed this article, please follow me for updates on new articles. Medium is where I want to give back to the community and help others learn coding. I welcome responses related to this article and its views. Please leave a response on what else you would like to learn and I’ll try to teach that at some point.

--

--