Email backend for Django which sends email via the Gmail API through a JSON credential
Project description
django-gmailapi-json-backend
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
:
- Add the module into the INSTALLED_APPS
INSTALLED_APPS = [ ... 'django-gmailapi-json-backend', ... ]
- 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
- 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)
- Enable the gmail api from the library menu
- On API and services > Credentials, create a new service account as a JSON you should use for GOOGLE_SERVICE_ACCOUNT
- 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.
- Move to the administrator console at https://admin.google.com/ and choose your user (i.e. EMAIL_FROM)
- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for django-gmailapi-json-backend-1.7.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c500c1df4ba7a5922fbb13dbe2d87c60722ae529601b2e1f0aa5bcddbc8705e8 |
|
MD5 | 9ef84c5f22695ab09192b0aee3cd2e53 |
|
BLAKE2b-256 | 8cf79223e6f8d3848c9827096026fd197540db06315e0c8809dff77a2a6a19cf |
Close
Hashes for django_gmailapi_json_backend-1.7-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17430e5b1df9d66419b26e53d76212db9f5a9d2b47306c934eceb6f2bc1757bc |
|
MD5 | 6aeaad70f01bb5665acddbdcbdb0c813 |
|
BLAKE2b-256 | 0ea04fda60a6ec513f973bad84539bd59505e00ca1c9d90410f7f9b55f5f9242 |