Django email backend using Microsoft Graph API (Application permissions)
Project description
django-msgraph-mail
A Django email backend for sending emails via the Microsoft Graph API. This package integrates seamlessly with Django's built-in email system, allowing you to send emails using Microsoft 365 accounts.
Features
- Simple integration with Django's email framework.
- Secure email delivery using Microsoft Graph API.
- Supports all standard Django email functions.
Prerequisites
Before installing and configuring django-msgraph-mail, ensure you have:
- A Django project (version 3.2 or higher recommended).
- A Microsoft 365 account with email capabilities.
- An Azure Active Directory (AAD) application registered with the following:
Mail.Sendpermission under Microsoft Graph API (Application permissions).- A client secret generated for the application.
- Access to the Tenant ID, Client ID, and Client Secret.
Installation
Install the package using pip:
pip install django-msgraph-mail
Configuration
To configure the email backend, add the following settings to your Django project's settings.py file:
EMAIL_BACKEND = "django_msgraph_mail.backend.GraphEmailBackend"
EMAIL_SENDER = "your-sender@domain.com"
CLIENT_ID = "your-azure-client-id"
CLIENT_SECRET = "your-azure-client-secret"
TENANT_ID = "your-tenant-id"
Configuration Details
EMAIL_SENDER: The email address of the Microsoft 365 account used to send emails (e.g.,sender@yourdomain.com).CLIENT_ID: The Application (client) ID of your Azure AD app.CLIENT_SECRET: A secret key generated for your Azure AD app.TENANT_ID: The Directory (tenant) ID of your Azure AD.
Security Recommendation
Store sensitive information like CLIENT_SECRET in environment variables to avoid exposing them in your codebase. Example:
import os
CLIENT_SECRET = os.getenv("AZURE_CLIENT_SECRET")
Setting Up Azure AD
- Go to the Azure Portal and sign in.
- Navigate to Azure Active Directory > App registrations > New registration.
- Register an application and note the Application (client) ID and Directory (tenant) ID.
- Under Certificates & secrets, create a new client secret and copy its value.
- Under API permissions, add the
Mail.Sendpermission for Microsoft Graph (Application permissions) and grant admin consent.
Usage
Once configured, you can use Django's built-in email functions to send emails. Example:
from django.core.mail import send_mail
send_mail(
subject="Welcome to My App",
message="Thank you for signing up!",
from_email="sender@yourdomain.com",
recipient_list=["recipient@example.com"],
fail_silently=False,
)
Additional Examples
Sending HTML Emails
from django.core.mail import EmailMultiAlternatives
email = EmailMultiAlternatives(
subject="HTML Email",
body="This is a plain text version.",
from_email="sender@yourdomain.com",
to=["recipient@example.com"],
)
email.attach_alternative("<p>This is an <b>HTML</b> email!</p>", "text/html")
email.send(fail_silently=False)
Sending to Multiple Recipients
send_mail(
subject="Newsletter",
message="Here's the latest news!",
from_email="sender@yourdomain.com",
recipient_list=["user1@example.com", "user2@example.com"],
fail_silently=False,
)
Troubleshooting
- Authentication Errors: Verify that
CLIENT_ID,CLIENT_SECRET, andTENANT_IDare correct. Ensure the Azure AD app has theMail.Sendpermission and admin consent. - Emails Not Sending: Check that the
EMAIL_SENDERmatches the Microsoft 365 account or a valid alias. Ensure the account has a valid license. - Network Issues: Confirm your server can access
https://graph.microsoft.com. - Debugging: Set
fail_silently=Falseto raise exceptions and check Django logs for detailed error messages.
Contributing
Contributions are welcome! Please submit issues or pull requests to the GitHub repository.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Support
For questions or support, open an issue on the GitHub repository or contact [django.infusion@gmail.com].
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_msgraph_mail-0.1.1.tar.gz.
File metadata
- Download URL: django_msgraph_mail-0.1.1.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
50d43a27d8c225e81dfbd38b017fb2f88a82cb082e65b19a358b55c8c65f8e0a
|
|
| MD5 |
b2f618f7c0f71f9f8a5cf28904be6ce3
|
|
| BLAKE2b-256 |
0a2d29da53534eac3e46d802a1e3ff2460b1661789b3055cc19ca4d6e9e9b180
|
File details
Details for the file django_msgraph_mail-0.1.1-py3-none-any.whl.
File metadata
- Download URL: django_msgraph_mail-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
049bec1a7d55ffc65693db4e1bc4199ec24141452ba336741767060f86ba3d1a
|
|
| MD5 |
0f5b777abf4de7ef4abc62824bcff49e
|
|
| BLAKE2b-256 |
3ce14b253e6e0e3a5c2041237d960fbd522ae7af528be63a2f551828aa5aca24
|