Skip to main content

django-gmailapi-json-backend

PyPI version Python 3.12

Email backend for Django which sends email via the Gmail API through a JSON credential

The simple SMTP protocol is disabled by default for Gmail users, since this is included in the Less Secure Apps (LSA) category. This package implements the Gmail API directly with a JSON Google service account as a Django email backend and can be used with 'django-mailer'.

Requirements

Python 3.12+

Installation

The package is available through pip. To easily install or upgrade it, do

pip install --upgrade django-gmailapi-json-backend

Configuration

In your settings.py:

  1. Add the module into the INSTALLED_APPS
    INSTALLED_APPS = [
        ...
        'gmailapi_backend',
        ...
    ]
    
  2. Set the email backend
    EMAIL_FROM = 'Company<your-email@domain.com>'
    GMAIL_USER = 'your-email@domain.com'
    EMAIL_BACKEND = "gmailapi_backend.service.GmailApiBackend"
    GMAIL_SCOPES = ['https://www.googleapis.com/auth/gmail.send']
    GOOGLE_SERVICE_ACCOUNT = '{
         "type": "service_account",
         "project_id": "your-project",
         "private_key_id": 
         ...
    }'
    
    If you use django-mailer as email backend you can send through gmail API as follow:
    EMAIL_BACKEND = "mailer.backend.DbBackend"
    MAILER_EMAIL_BACKEND = "gmailapi_backend.service.GmailApiBackend"
    

How to create the Google service account

  1. Create a project on the developer console at https://console.cloud.google.com (it must be a Google Workspace account, not a simple gmail account)
  2. Enable the gmail api from the library menu
  3. On API and services > Credentials, create a new service account as a JSON you should use for GOOGLE_SERVICE_ACCOUNT
  4. Copy your client id from the menu IAM and administration > service account. Click on the service you have just created, find the unique id and copy it.
  5. Move to the administrator console at https://admin.google.com/ and choose your user (i.e. EMAIL_FROM)
  6. Go to Security > Data access and control > API controls > Delegation at domain level and add a new one with your client id and the services you need like https://www.googleapis.com/auth/gmail.send to send email through API.

Usage

Use the native EmailMessage class in Django. Just a sample:

message = render_to_string('email/ordine_pagato.html', {
  'ordine': ordine,
})
mail_subject = _('This is just a sample')
email = EmailMessage(
  mail_subject, message, settings.EMAIL_FROM, to=['recipient@domain.com']
)
email.content_subtype = "html"
email.attach(sample_file.file.name, sample_file.file.read(), 'application/pdf')
email.send()

Metadata

Release files for django-gmailapi-json-backend 1.12

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

Source distribution (sdist)

Source distribution for django-gmailapi-json-backend 1.12
File Size Uploaded
django_gmailapi_json_backend-1.12.tar.gz 8.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-gmailapi-json-backend 1.12
File Interpreter ABI Platform
django_gmailapi_json_backend-1.12-py3-none-any.whl Python 3 none any Details

Total release size: 17.4 kB

Release files / django_gmailapi_json_backend-1.12.tar.gz

Download URL django_gmailapi_json_backend-1.12.tar.gz
Size 8.7 kB
Tags Source
SHA-256 checksum
How to use checksums
4ee21510177d424ebe6b6695468c6970da2d35a689f7b75debdcd39b6609b715
BLAKE2b-256 checksum
How to use checksums
cfad9111c4706fb6bc6e898b9ee28b21fbf5295a9247164fc32390ff7cfd5a52
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.21

Release files / django_gmailapi_json_backend-1.12-py3-none-any.whl

Download URL django_gmailapi_json_backend-1.12-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e68b323bde13bb055ccd2300f732d300e7796a4221cce913cc8541b2293d150f
BLAKE2b-256 checksum
How to use checksums
abcd0cbb8765656a55ee7fae03ae931c7e485553f14f56c182f8efd9141c9f58
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.21

Release history Release notifications | RSS feed

This release

1.12 This release

2 release files

1.11

1 release file

1.10

2 release files

1.9

2 release files

1.8

2 release files

1.7

2 release files

1.6

2 release files

1.5

2 release files

1.4

2 release files

1.3

2 release files

1.2

2 release files

1.1

2 release files

1.0

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