Sending Emails using python after May 2022

Vishal Bulbule
2 min readDec 31, 2022

--

Introduction

This article explained the way to send emails using python with Gmail SMTP server after May 2022. In May 2022 Google disabled the “less secure apps” feature from the Gmail setting due to security reasons. After that its been difficult to use the Gmail SMTP server for programming.

What has changed?

Please find below a message from Google.

Alternate Solution

  1. No change in the actual python code.
import smtplib

sender_email = "abc@gmail.com"
receiver_email = "xyz@gmail.com"

#create connection
connection = smtplib.SMTP("smtp.gmail.com","587")

#start TLS
connection.starttls()
print("Connection made successfully")


#login with app password
connection.login(user=sender_email,password="dygjhfbwottlxxtf")
print("Logged in successfully")

connection.sendmail(from_addr=sender_email,to_addrs=receiver_email,msg="subject:Email Test\n\nPython email Testing")
print("sent email successfully")

connection.close()
print("Closed connection in successfully")
  1. Users will usually face the below error.

3. To resolve this error create the application password in your Google account using the below steps and use the application password in python.

Create an Application password in Gmail

  1. Go to Manage Google account > Security tab.

2. Enable 2-Step verification.

3. Once Enabled, you will see App password Tab as shown below.

4. Open the App passwords Tab and generate 16 digit password.

5. Copy this 16-digit password and use as it is in python code.

6. Try sending an email.

7. Delete app password after use to secure Google account.

Please refer below Video for Demo

--

--

Vishal Bulbule
Vishal Bulbule

Written by Vishal Bulbule

Google Cloud Architect || Believe in Learn , work and share knowledge ! https://www.youtube.com/@techtrapture

No responses yet