This is a simple Flask application that uses OAuth 2.0 for Google login and Gmail API to send emails. The application includes three main pages: the email form, the email preview, and a confirmation page upon successful email sending.
FLASK MAILER TEST
│
├── templates
│ ├── index.html
│ ├── preview.html
│ └── sent.html
│
├── .env
├── .gitignore
├── app.py
└── requirements.txt
git clone https://github.com/marketcalls/flask-gmail-oauth.git
cd flask-gmail-oauth
python3 -m venv venv
source venv/bin/activate # On Windows use `venv\Scripts\activate`
pip install -r requirements.txt
Create a .env
file in the root directory with the following content:
SECRET_KEY=your_secret_key
[email protected]
GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_client_secret
OAUTH_ACCESS_TOKEN_URL=https://oauth2.googleapis.com/token
OAUTH_AUTHORIZE_URL=https://accounts.google.com/o/oauth2/auth
OAUTH_SCOPE=openid email https://www.googleapis.com/auth/gmail.send https://www.googleapis.com/auth/userinfo.email
OAUTH_USERINFO_ENDPOINT=https://www.googleapis.com/oauth2/v3/userinfo
OAUTH_JWKS_URI=https://www.googleapis.com/oauth2/v3/certs
GMAIL_API_SEND_URL=https://www.googleapis.com/gmail/v1/users/me/messages/send
Create a new project in Google Cloud Console
Enable Gmail API
Create OAuth 2.0 credentials
http://127.0.0.1:5000
to "Authorized JavaScript origins"http://127.0.0.1:5000/login/authorized
to "Authorized redirect URIs".env
fileYou can use the test email IDs for testing the application in the oAuth Consent screen then scroll down to the Test users section and add the test users
python app.py
Open your web browser and go to http://127.0.0.1:5000/login
to login to application using gmail
and http://127.0.0.1:5000/
to send email
This project is licensed under the MIT License.