Skip to main content

django-gov-notify, a GOV.UK Notify flavoured Django email backend

GitHub workflow

django-gov-notify provides Django integration with the GOV.UK Notify service for sending emails and SMS messages. Python Client Documentation.

This is implemented as a custom Django email backend. It presents a similar internal API to standard Django email backends, but with some restrictions:

  • GOV.UK Notify emails are sent to one recipient each. CC: and BCC: fields are not supported.
  • A single email 'message' with multiple recipients will result in multiple individual API calls to GOV.UK Notify, each message being sent to a single recipient. The backend will still report back 1, as per Django's default behaviour.
  • Attachments are not (at the moment) supported.
  • Custom headers are not supported.
  • To configure a 'reply-to' address, you must first configure such an address in the GOV.UK Notify admin interface.
  • The 'from' address field is not supported. This must be configured within the GOV.UK Notify admin interface.
  • Preformatted emails are expected to be configured in the service admin dashboard as Markdown templates with placeholders.
  • The email body is interpreted as very limited Markdown. On testing, it seems that variables are not interpreted as markdown, or maybe mangled, e.g. _test_ was emailed as *test*.

Compatibility

django-gov-notify supports:

  • Python 3.10, 3.11, 3.12, 3.13 and 3.14
  • Django 4.2, 5.2 and 6.0

Installation

Using pip:

$ pip install django-gov-notify

Using Poetry

$ poetry add django-gov-notify

Configuration

In your Django project's settings:

EMAIL_BACKEND = "django_gov_notify.backends.NotifyEmailBackend"

You will need at least one email template ID, with a plain template:

Subject: ((subject))
Body: ((body))

Set the Django settings:

  • GOVUK_NOTIFY_API_KEY (NB not GOV_UK…)
  • GOVUK_NOTIFY_PLAIN_EMAIL_TEMPLATE_ID

This plain template ID setting, and template IDs passed to the NotifyEmailMessage class, use string representations of the UUID keys.

Usage

Sending an email using a template

Configure the template in the GOV.UK Notify dashboard:

Subject: Message about ((topic))
Body: Hello ((first name)), your reference is ((ref number))

Create an email message, supplying the template ID and a personalisation dictionary (this should also include any variables defined in the template subject):

from django_gov_notify.message import NotifyEmailMessage

message = NotifyEmailMessage(
    to=["recipient@example.com"],
    template_id="43573f75-80e7-402f-b308-e5f1066fbd6f",
    personalisation={
        "topic": "The Prisoner",
        "first name": "Patrick",
        "ref number": "6",
    },
)
message.send()

Note that in this case a subject and body are not required, nor permitted, because the personalisation dict won't know how to do anything with them.

Sending an email using the default (blank) template

This assumes you have configured a blank template with the parameters

Subject: ((subject))
Body: ((body))

from django_gov_notify.message import NotifyEmailMessage

message = NotifyEmailMessage(
    subject="Test subject", body="Test message content", to=["recipient@example.com"]
)
message.send()

Note that in this case a subject and body are required, and you must not pass the template_id or personalisation kwargs.

Sending an email using the send_mail shortcut function

Use it in the normal fashion, including a 'from' address that will be discarded:

from django.utils.mail import send_mail

send_mail("Subject", "Message content", "from@example.com", ["recipient@example.com"])

This will use the blank template ID configured as settings.GOVUK_NOTIFY_PLAIN_EMAIL_TEMPLATE_ID. Attachments, custom headers, and BCC recipients are not supported.

Contributing

To work on this repository locally:

  • install: poetry install
  • run tests: poetry run python runtests.py

Release files for django-gov-notify 0.7.0

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-gov-notify 0.7.0
File Size Uploaded
django_gov_notify-0.7.0.tar.gz 5.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-gov-notify 0.7.0
File Interpreter ABI Platform
django_gov_notify-0.7.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.2 kB

Release files / django_gov_notify-0.7.0.tar.gz

Download URL django_gov_notify-0.7.0.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
86ba75d4c7b112bd6f6ff73a16b1768ddfa93900bfb9759b9e74286edcb6c51d
BLAKE2b-256 checksum
How to use checksums
9985b193b53e8f23c442aadfd96f595fb1c46fea83f258c217f32c9391caac5f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.12.2 Linux/6.8.0-106-generic

Release files / django_gov_notify-0.7.0-py3-none-any.whl

Download URL django_gov_notify-0.7.0-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
02312ebfe337c6276ae5a721142f41e640cf576059b9b549cdcdda0e051df4f8
BLAKE2b-256 checksum
How to use checksums
89d2b632a7461a5d107ab1a393e9f4105067f3efeac34a52f82c4c3de05cdea1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.12.2 Linux/6.8.0-106-generic

Release history Release notifications | RSS feed

This release

0.7.0 This release

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

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