Skip to main content

django-o365mail

About

SMTP authenthication is getting increasingly difficult, mainly because of security.

This Django email backend has been created to solve that problem when using Office 365. It's easy to use and quite customizable.

Installation

Run the following on your system:

pip install django-o365mail

Then, add these settings to your Django settings.py:

EMAIL_BACKEND = 'django_o365mail.EmailBackend'

O365_MAIL_CLIENT_ID = 'REPLACE THIS'
O365_MAIL_CLIENT_SECRET = 'REPLACE THIS'
O365_MAIL_TENANT_ID = 'REPLACE THIS'

If you're using git, make sure to add the following to .gitignore:

o365_token.txt

Mail can then be sent using the Django mail functions, as described here.

Optional settings

Mailbox

This module uses the python-o365 library, which is also slightly customizable. Because of this you can define kwargs to be used when opening the mailbox. As an example:

O365_MAIL_ACCOUNT_KWARGS = {'token_backend': 'O365.utils.token.EnvTokenBackend'}
O365_MAIL_MAILBOX_KWARGS = {'resource': 'o365mailbox@domain.com'}

... which will be passed to the mailbox() function like this:

account = O365.Account(credentials, auth_flow_type='credentials', tenant_id=self.tenant_id, **account_kwargs)
mailbox = account.mailbox(**O365_MAIL_MAILBOX_KWARGS)

Other

  • O365_MAIL_REPLACE_LINE_ENDINGS (default: True)
    Replaces line endings with an HTML line break (<br />) when emails are sent in plain text.
  • O365_MAIL_SAVE_TO_SENT (default: False)
    Saves sent emails to the 'Sent' folder.
  • O365_SUBJECT_PREFIX (default: '')
    Prefix for the subject of any email sent using this backend.
  • O365_ACTUALLY_SEND_IN_DEBUG (defaullt: False)
    The default value prevents sending when DEBUG=True. You most likely want to set this to True for development purposes.

Sandbox

Create a file called settings_secret.py under the sandbox/sandbox directory (same directory as settings.py) with the settings as described above, except for the EMAIL_BACKEND. It could look like this:

O365_MAIL_CLIENT_ID = 'REPLACE THIS'
O365_MAIL_CLIENT_SECRET = 'REPLACE THIS'
O365_MAIL_TENANT_ID = 'REPLACE THIS'

Then, if needed, add settings to settings.py.

To be able to make changes to this module, run the following command to install this module in editable mode:

pip install -e ./

And run the sandbox server:

cd sandbox && python manage.py runserver

Metadata

Release files for django-o365mail 1.1.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-o365mail 1.1.0
File Size Uploaded
django-o365mail-1.1.0.tar.gz 8.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-o365mail 1.1.0
File Interpreter ABI Platform
django_o365mail-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.8 kB

Release files / django-o365mail-1.1.0.tar.gz

Download URL django-o365mail-1.1.0.tar.gz
Size 8.6 kB
Tags Source
SHA-256 checksum
How to use checksums
dc8b18f7bc4f488674dd6ef0dedc19a479cd646133afb7643b6d914bb6bc6b8c
BLAKE2b-256 checksum
How to use checksums
0ee2f2df6ff046aff30ab74db2cb98615dc04ae96d58c9a20b7c3db7d23e2c8d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.10

Release files / django_o365mail-1.1.0-py3-none-any.whl

Download URL django_o365mail-1.1.0-py3-none-any.whl
Size 9.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
13ed62247b757c59e020da69b33a021f362f1cf860b54d55d7ebb5ef291358ad
BLAKE2b-256 checksum
How to use checksums
a9ca84b111cd0713f91bb8b136e12244ca6b20ce4cb01576be67a746f879c9da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.10

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.1

2 release files

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