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
- Visit: https://console.cloud.google.com/apis/credentials while logged in to your Gmail Account
- Create a new project for your application that is to send emails
- Visit: https://console.developers.google.com/start/api?id=gmail to enable Gmail
- Configure the OAuth consent screen: https://console.cloud.google.com/projectselector2/apis/credentials/consent?supportedpurview=project
- Ensure the
https://www.googleapis.com/auth/gmail.sendscope is enabled - Create OAuth 2.0 credentials (select "Web application" as the application type) and save the JSON data
- 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.
- Get a refresh token using the OAuth 2.0 Playground: https://developers.google.com/oauthplayground/
- Click on the gear icon in the upper right corner of the OAuth 2.0 Playground to open the OAuth 2.0 Configuration.
- 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.
- Select the
https://www.googleapis.com/auth/gmail.sendscope. - 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.
- Click "Exchange authorization code for tokens" to get your access token and refresh token.
- (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!")
Metadata
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)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| gmail_python_client-0.0.4.tar.gz | 5.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| 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
|
Security for gmail-python-client 0.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.