Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

django-gmailapi-json-backend

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

django-gmailapi-json-backend

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

  • 1.10
  • PyPI
  • Socket score

Maintainers
1

django-gmailapi-json-backend

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

Requirements

Python 3.9+

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

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc