Skip to main content

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

Project description


PyPI version Python 3.9 Python 3.10

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'.


Python 3.9+


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

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


In your

  1. Add the module into the INSTALLED_APPS
  2. Set the email backend
    EMAIL_FROM = 'Company<>'
    GMAIL_USER = ''
    EMAIL_BACKEND = "gmailapi_backend.service.GmailApiBackend"
    GMAIL_SCOPES = ['']
         "type": "service_account",
         "project_id": "your-project",
    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 (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 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 to send email through API.


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=['']
email.content_subtype = "html"
email.attach(,, 'application/pdf')

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

django-gmailapi-json-backend-1.7.tar.gz (8.1 kB view hashes)

Uploaded source

Built Distribution

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