Skip to main content

Client for Sending Emails via GMail using OAuth2.0

Project description

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!")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gmail_python_client-0.0.4.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

gmail_python_client-0.0.4-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file gmail_python_client-0.0.4.tar.gz.

File metadata

  • Download URL: gmail_python_client-0.0.4.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for gmail_python_client-0.0.4.tar.gz
Algorithm Hash digest
SHA256 a46c84a9d0aee7d5d001c2b4814bbfa26499e436ca347899043d065541ade983
MD5 60077dcce8aca098ee37360689cc4268
BLAKE2b-256 44296b32063a2c38cdb4a7a625ff2aad083df9d8e853ec336e916711014232f9

See more details on using hashes here.

Provenance

File details

Details for the file gmail_python_client-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for gmail_python_client-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 c7c2ac3aedb9a0c28feb1e7aa5d7507e2b62f0d69fb3024099d2d8ece825b913
MD5 d3904ac713acefc4c725812fc7d07e87
BLAKE2b-256 8b0d931c0bc9d6371b1df3cb200e8d8e9e7974218d2235b6ada1cf74dc4482dd

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page