Introduction: Send Email Using Python!

About: We are two brothers who like to make things.

In this beginner tutorial I will show you how to use Python programming language to write a simple command line app for sending emails via Gmail!

You will only need 11 lines of code!

Step 1: Download Python

Download Python 3.6.0 from https://www.python.org/downloads/

Step 2: Install Python & Add to Path

Open up the python installer you just downloads.

Click install, but make sure to check the checkboxes that say "Add Python to PATH" and "Install Pip"

Step 3: Install SMTPLib

  1. Open up your computer's command prompt.

On my Windows PC, this is done by going to Start, then typing in "CMD", clicking the "CMD" app to open it up.

2. In the command prompt, type in:

pip install smtplib

Then hit enter.

It will then install the library needed to connect Gmail to Python.

Step 4: Configure Gmail

  1. While logged into your gmail at gmail.com, go to https://myaccount.google.com/security
  2. Scroll down to the part that says "Allow less secure apps"
  3. Turn ON "allow less secure apps".

If you don't want to make your main gmail less secure, or if you don't already have gmail, then sign up for a new gmail solely for this purpose.

Step 5: Download the Python Script

Download the attached gmailpython.py file to a location on your computer that is easy to remember.

Or, simply copy the identical code below and paste it into your favorite text editor, then save it as "gmailpython.py" with UTF-8 encoding:

import smtplib 
gmailaddress = input("what is your gmail address? \n ")
gmailpassword = input("what is the password for that email address? \n  ")
mailto = input("what email address do you want to send your message to? \n ")
msg = input("What is your message? \n ")
mailServer = smtplib.SMTP('smtp.gmail.com' , 587)
mailServer.starttls()
mailServer.login(gmailaddress , gmailpassword)
mailServer.sendmail(gmailaddress, mailto , msg)
print(" \n Sent!")
mailServer.quit()

Step 6: Run the Script!

Open up command prompt the same way you did earlier.

type in cd ____

with the path to gmailpython.py replacing the blank line.

For example, on my laptop the command is

cd c:\users\donovan\downloads

Hit enter, then type in

py gmailpython.py

And hit enter again.

The command line app will now load, and it will prompt you for the your login details, your message, and who you want to send the message to!

Step 7: How It Works

import smtplib

That line above loads to smtplib library, which adds gmail integration to python.

gmailaddress = input("what is your gmail address? \n ")
gmailpassword = input("what is the password for that email address? \n  ")
mailto = input("what email address do you want to send your message to? \n ")
msg = input("What is your message? \n ")

These lines display a prompt for input, and store your answers in variables named "gmailaddress", "gmailpassword", "mailto", and "msg". Think of variables as nicknames for your input.

The "/n" tells the script to display a new line before the next command.

mailServer = smtplib.SMTP('smtp.gmail.com' , 587)
mailServer.starttls()

The part above sets up the connection to the gmail mail server.

mailServer.login(gmailaddress , gmailpassword)

That part logs Python in to your Gmail account, taking the info from the variables that store your previous answers.

mailServer.sendmail(gmailaddress, mailto , msg)

That part sends the email message using the info from the variables that store your info.

print(" \n Sent!")

That part replies "Sent!" in the command line so you can know the code made it to that point.

Note: In Python 2.7, you don't need the parentheses around the quotes. We are using Python 3.6, which does require parentheses.

mailServer.quit()

The final part quits the connection to the mail server.

Step 8: That's All!

I hope this was interesting and educational to you.