8

@oauth2_gmail_python

An open source function to send emails using Gmail and generate OAUTH2 Tokens in Python.

An open source function to send emails using Gmail and generate OAUTH2 Tokens in Python.

Requirements

  • Gmail & Google Developers/Gcloud Account
  • Enable Gmail API and grant Send Email scope in your Google Developers/Gcloud Account
  • Python 3
  • Pip

Install

  1. Create a virtual environment
python -m venv venv
  1. Activate the virtual environment
./venv/scripts/activate
  1. Install requirements
pip install -r requirements.txt
  1. Replace EMAIL_FROM with your gmail (or other provider's) email address.
    EMAIL_FROM = "youremail@gmail.com"
  1. Replace the following variables using credentials from your Google Developers/Gcloud Account
REDIRECT_URI = "--------"
CLIENT_ID = "-----------.apps.googleusercontent.com"
CLIENT_SECRET = "---------------"

Usage

  1. Run the app
python app.py
  1. Go through each option starting from #1
Select an option
 
1. Generate Authorization Code
2. Generate Tokens
3. Send Email
  1. For Option #1 You will go through the following steps on your browser when it generates a URL

oath2_1

oath2_2

oath2_3

  1. Replace the AUTHORIZATION_CODE variable from 'code' in the redirected url

oath2_4

AUTHORIZATION_CODE = ""
  1. Run the app again and select the remaining 2 options
python app.py
  1. Check your email for the successful result!

oath2_5