Skip to main content

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

Project description

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()

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.12.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_gmailapi_json_backend-1.12-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file django_gmailapi_json_backend-1.12.tar.gz.

File metadata

File hashes

Hashes for django_gmailapi_json_backend-1.12.tar.gz
Algorithm Hash digest
SHA256 4ee21510177d424ebe6b6695468c6970da2d35a689f7b75debdcd39b6609b715
MD5 bd0ebd732720b0f69449f156a9e31899
BLAKE2b-256 cfad9111c4706fb6bc6e898b9ee28b21fbf5295a9247164fc32390ff7cfd5a52

See more details on using hashes here.

File details

Details for the file django_gmailapi_json_backend-1.12-py3-none-any.whl.

File metadata

File hashes

Hashes for django_gmailapi_json_backend-1.12-py3-none-any.whl
Algorithm Hash digest
SHA256 e68b323bde13bb055ccd2300f732d300e7796a4221cce913cc8541b2293d150f
MD5 0c7fe7fa66c8db01bff1de7bde753a0b
BLAKE2b-256 abcd0cbb8765656a55ee7fae03ae931c7e485553f14f56c182f8efd9141c9f58

See more details on using hashes here.

Supported by

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