Send Gmail Notification Using Python

Sharing is caring!

Last Updated on July 14, 2022 by Jay

We will create a Python program to automatically send ourselves an email/Gmail notification when our Amazon price notification bot finds a price drop.

In the first part, we created a simple bot using Selenium to find pricing information from an online shopping site. Although we used Amazon as an example, the same technique should work on 99.968% of sites.

Everyone has their own preferences so I’m going to show two ways to automatically send a notification, this tutorial covers sending Gmail, the next one will cover sending notifications on Whatsapp.

Check out the following links if you need help with any of the steps.

Tools Required

We need only two standard Python libraries to achieve automatically sending emails/Gmail with Python, no additional pip install is required.

  1. email – This library is used for creating an email.
  2. smtplib – SMTP stands for “Simple Mail Transfer Protocol”, which is used for sending an email. I once learned an easy way to remember this: Send Mail To People. Okay, let’s see how we can use Python to SMTP automatically.

Create An Email In Python

The legacy way of creating an email with Python is by using the email.mime multipart object, which is less intuitive than the latest approach. To use the latest email methods, we need Python v3.6 and above. Personally, I prefer the new way.

We create an EmailMessage object, then simply treat it as a dictionary and feed in values for the subject, from, to. The body of the email is added by using the .set_content method. I’m using the same email address to send & receive notifications to myself, so the From and To are the same, but they don’t have to.

from email.message import EmailMessage

url = 'https://www.amazon.ca/Logitech-Master-Advanced-Wireless-Mouse/dp/B07S395RWD/ref=sr_1_4?dchild=1&keywords=mouse%2Bmx3&qid=1634182753&sr=8-4&th=1'
msg = EmailMessage()
msg['Subject'] = 'Notification from Python'
msg['From'] = 'amznbotnotification@gmail.com'
msg['To'] = 'amznbotnotification@gmail.com'
msg.set_content(url)

Send A Gmail Using Python

We need to change a setting in the Gmail account to ensure we can send emails. Head to this website: https://myaccount.google.com/lesssecureapps

Make sure the setting is turned ON. If not, Python cannot log in to your Gmail account.

Less Secure App Access Turned ON

In order to send emails, we need to encrypt the data before sending it. That’s where the SSL (Secure Sockets Layer) comes into play. The smtplib provides a convenient way for encrypting data, which is the SMTP_SSL class. When we create an SMTP_SSL object, a secure communication channel is created before sending the email.

We also need to connect to Gmail’s SMTP server ‘smtp.gmail.com’, with a port 465. Once we created the SMTP_SSL object and connected to Gmail server, we need to log in to our Gmail account by .login(user_name, passwd). Make sure to use your own login credential here. Then we can .send_message(msg) to send out the msg that we created using the email library.

import smtplib

smtp = smtplib.SMTP_SSL('smtp.gmail.com', 465)   ## Connect to Gmail SMTP server
smtp.login('amznbotnotification@gmail.com', {password})  ## Login
smtp.send_message(msg)
smtp.close()

To ensure we always close the SMTP connection and less typing, we can use a context manager:

import smtplib

with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
    smtp.login('amznbotnotification@gmail.com', {password})
    smtp.send_message(msg)

DO NOT SAVE PASSWORD IN YOUR CODE

I know someone’s probably going to yell at me for storing a password in the Python script. It’s okay to do so if you are 100% sure that you won’t ever share this code with anyone else. But in reality, we might forget that we had saved some passwords in a file that we worked on a long time ago, so better to be safe than sorry.

We’ll take an extra step and save our Gmail password into the system environment variable.

To do that, type “PATH” into the Windows search bar, then Environment Variables… Click on “New” to create a new User variable, then enter the variable name & value. OK to save.

Creating a new user environment variable

To retrieve the password from the environment variable, we can use the os Python library like the following.

import os
os.environ.get('EMAIL_PASSWD')

Putting It Together

We want the bot to check the price and if there’s a sale going on, we’ll send ourselves a notification. It means that we don’t send the notification every time we check the price, so we need a condition if price < 129.99. Check part 1 of the series if you need help finding the price and name of the product.

from email.message import EmailMessage
import os
import smtplib

url = 'https://www.amazon.ca/Logitech-Master-Advanced-Wireless-Mouse/dp/B07S395RWD/ref=sr_1_4?dchild=1&keywords=mouse%2Bmx3&qid=1634182753&sr=8-4&th=1'

if price < 129.99:
    msg = EmailMessage()
    msg['Subject'] = f'Price drop for {name} ----- {price}'
    msg['From'] = 'amznbotnotification@gmail.com'
    msg['To'] = 'amznbotnotification@gmail.com'
    msg.set_content(url)

    with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
        smtp.login('amznbotnotification@gmail.com', {password})
        smtp.send_message(msg)

In the next tutorial, we’ll use Python to send ourselves a notification on Whatsapp. You know, some people don’t check their email often so we gotta have multiple channels of communication to make sure we get the price drop notification while the sale lasts!

Leave a Reply

Your email address will not be published. Required fields are marked *