Skip to main content

Gmail-Python-Client

Client for Sending Emails via GMail using OAuth2.0 for Python https://developers.google.com/identity/protocols/oauth2

Configuring a new project

Installing

pip install gmail-python-client

Google Cloud Configuration

  1. Visit: https://console.cloud.google.com/apis/credentials while logged in to your Gmail Account
  2. Create a new project for your application that is to send emails
  3. Visit: https://console.developers.google.com/start/api?id=gmail to enable Gmail
  4. Configure the OAuth consent screen: https://console.cloud.google.com/projectselector2/apis/credentials/consent?supportedpurview=project
  5. Ensure the https://www.googleapis.com/auth/gmail.send scope is enabled
  6. Create OAuth 2.0 credentials (select "Web application" as the application type) and save the JSON data
  7. Make sure that the redirect URI (https://developers.google.com/oauthplayground) is added to the list of authorized redirect URIs in your project's OAuth 2.0 Client ID settings in Google Cloud Console.
  8. Get a refresh token using the OAuth 2.0 Playground: https://developers.google.com/oauthplayground/
  9. Click on the gear icon in the upper right corner of the OAuth 2.0 Playground to open the OAuth 2.0 Configuration.
  10. Ensure that "Use your own OAuth credentials" is checked and fill in the "OAuth Client ID" and "OAuth Client secret" that you obtained from the Google Cloud Console.
  11. Select the https://www.googleapis.com/auth/gmail.send scope.
  12. Also, make sure the checkbox "Auto-refresh the token before it expires" is unchecked, because you need the refresh token to be able to generate new access tokens in your application.
  13. Click "Exchange authorization code for tokens" to get your access token and refresh token.
  14. (Note:) You can use the API Playground to generate an access token without creating an OAuth2.0 client ID directly, but it will only last 24 hours

Application Configuration

Once you have a 24hour access token from the API Playground or a more permanenent combination of a Client ID, Client Secret and Refresh Token, you can use this library like so:

from gmail_python_client import GmailClient

# Configure with Access Token
# Looks like:
# ya29.a0AfB_byDzLjqCbsdb_RMnguTrB8fmdSEsOSMkvWH6zjcm-1UIi3cdNpfvhZUjYZLVB7NrnNGJIEEatntXjBpc5Bk_cIGKgnjqtesO-HLT0H2Yiz-lZFviz3_UfaaoR8HLjmrKmm7VCkBZvdI0ABb4ADnY9fEKxcLMhR4daCgYKAQcSARASFQGOcNnCvZ0k13Q_KYFObZFjh5umXQ0171
if __name__ == '__main__':
    c = GmailClient(
        "my_sender_email@gmail.com",
        access_token="<access_token>",
    )

    # Send the email to pswanson@ucdavis.edu
    c.send_email("pswanson@ucdavis.edu", "Test", "Hello World!")
from gmail_python_client import GmailClient

# Configure with Refresh Token
# Looks like:
# 1//04mDFqbnpQnGTCgYIARAAGAQSNwF-L9IruAAqiKvHXfHzZnMt6UqTzDOZGg4TJha3oCGa9utu_PwxfrmG-su47Qytt8m2eWDDwZo
if __name__ == '__main__':
    c = GmailClient(
        "my_sender_email@gmail.com",
        client_id="<client_id>",
        client_secret="<client_secret>",
        refresh_token="<refresh_token>",
    )

    # Send the email to pswanson@ucdavis.edu
    c.send_email("pswanson@ucdavis.edu", "Test", "Hello World!")

Configuring via the Environment

You can configure the client using environmental variables rather than passing the configuration to the constructor:

# Configure with Access Token
export GMAIL_SENDER_EMAIL_ADDRESS="my_sender_email@gmail.com"
export GMAIL_OAUTH_ACCESS_TOKEN="ya29.a0AfB_byDzLjqCbsdb_RMnguTrB8fmdSEsOSMkvWH6zjcm-1UIi3cdNpfvhZUjYZLVB7NrnNGJIEEatntXjBpc5Bk_cIGKgnjqtesO-HLT0H2Yiz-lZFviz3_UfaaoR8HLjmrKmm7VCkBZvdI0ABb4ADnY9fEKxcLMhR4daCgYKAQcSARASFQGOcNnCvZ0k13Q_KYFObZFjh5umXQ0171"
from gmail_python_client import GmailClient

if __name__ == '__main__':
    # Send the email to pswanson@ucdavis.edu
    GmailClient().send_email("pswanson@ucdavis.edu", "Test", "Hello World!")
# Configure with Refresh Token
export GMAIL_SENDER_EMAIL_ADDRESS="my_sender_email@gmail.com"
export GMAIL_OAUTH_REFRESH_TOKEN="1//04mDFqbnpQnGTCgYIARAAGAQSNwF-L9IruAAqiKvHXfHzZnMt6UqTzDOZGg4TJha3oCGa9utu_PwxfrmG-su47Qytt8m2eWDDwZo"
export GMAIL_OAUTH_CLIENT_ID="MyClientID.apps.googleusercontent.com"
export GMAIL_OAUTH_CLIENT_SECRET="GOCYYW-J2krl75t71RhZdZmmB-bSRX52lhJ"
from gmail_python_client import GmailClient

if __name__ == '__main__':
    # Send the email to pswanson@ucdavis.edu
    GmailClient().send_email("pswanson@ucdavis.edu", "Test", "Hello World!")

Release files for gmail-python-client 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gmail-python-client 0.0.4
File Size Uploaded
gmail_python_client-0.0.4.tar.gz 5.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for gmail-python-client 0.0.4
File Interpreter ABI Platform
gmail_python_client-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 12.6 kB

Release files / gmail_python_client-0.0.4.tar.gz

Download URL gmail_python_client-0.0.4.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
a46c84a9d0aee7d5d001c2b4814bbfa26499e436ca347899043d065541ade983
BLAKE2b-256 checksum
How to use checksums
44296b32063a2c38cdb4a7a625ff2aad083df9d8e853ec336e916711014232f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release files / gmail_python_client-0.0.4-py3-none-any.whl

Download URL gmail_python_client-0.0.4-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c7c2ac3aedb9a0c28feb1e7aa5d7507e2b62f0d69fb3024099d2d8ece825b913
BLAKE2b-256 checksum
How to use checksums
8b0d931c0bc9d6371b1df3cb200e8d8e9e7974218d2235b6ada1cf74dc4482dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page