directus-extension-msgraph-mail
v1.0.5
Published
Send all Directus emails via Microsoft Graph API with OAuth2 Client Credentials, including full Liquid template support. Compatible with Directus 10 and 11.
Maintainers
Readme
Directus Extension: Microsoft Graph Mail
Send all Directus emails via Microsoft Graph API using OAuth2 Client Credentials flow. This extension replaces the default Directus email transport with Microsoft Graph API, enabling seamless integration with Microsoft 365 / Azure AD — including full support for Directus Liquid mail templates (password reset, invitations, custom templates).
Features
- 🔐 OAuth2 Client Credentials Flow - Secure authentication using Azure AD App Registration
- 📧 Automatic Email Interception - All Directus emails are automatically sent via Microsoft Graph
- 🧩 Liquid Template Rendering - Renders Directus' built-in mail templates (password reset, user invitation, etc.) plus your own custom templates with the same
{{ projectName }},{{ projectLogo }},{{ projectColor }}and{{ projectUrl }}defaults Directus provides out of the box - 🎯 Manual Email Operation - Includes a Directus Flow operation for sending emails programmatically
- 🔄 Token Caching - Automatic token refresh with 5-minute buffer
- 📝 Rich Email Support - HTML and plain text emails with CC/BCC and attachments
- ⚡ Zero Configuration - Works automatically once environment variables are set
Installation
Via NPM
npm install directus-extension-msgraph-mailManual Installation
- Download the latest release
- Extract to your Directus
extensionsfolder - Restart Directus
Note on sandbox mode: Starting with
1.0.4this extension runs outside the Directus sandbox. The sandbox does not allow access tonode:fs/node:module, both of which are required to locate and render Directus' built-in Liquid mail templates. Because the extension is no longer sandboxed, it is not eligible for Marketplace distribution and must be installed via NPM or manually. Network and filesystem access is limited to what is needed for token acquisition, the Graph API and the local mail templates folder.
Configuration
1. Azure AD App Registration
Create an App Registration in Azure AD with the following settings:
- Go to Azure Portal → Azure Active Directory → App registrations
- Click New registration
- Set a name (e.g., "Directus Mail Sender")
- Click Register
- Note down the Application (client) ID and Directory (tenant) ID
- Go to Certificates & secrets → New client secret
- Create a secret and note down the Value (not the Secret ID!)
- Go to API permissions → Add a permission → Microsoft Graph → Application permissions
- Add the following permission:
Mail.Send- Send mail as any user
- Click Grant admin consent (requires admin privileges)
2. Environment Variables
Add the following environment variables to your Directus .env file:
# Microsoft Graph Configuration
MSGRAPH_TENANT_ID=your-tenant-id
MSGRAPH_CLIENT_ID=your-client-id
MSGRAPH_CLIENT_SECRET=your-client-secret
[email protected]
# Optional: Fallback sender email (used if MSGRAPH_SENDER_EMAIL is not set)
[email protected]
# Optional: Custom directory with additional Liquid mail templates (.liquid)
# Defaults to Directus' built-in templates folder if not set.
EMAIL_TEMPLATES_PATH=/directus/templates3. Restart Directus
# Docker
docker compose restart backend
# PM2
pm2 restart directus
# Node
# Stop and start your Directus instanceUsage
Automatic Email Interception (Hook)
Once configured, all Directus emails are automatically sent via Microsoft Graph API:
- Password reset emails
- User invitations
- Notification emails
- Custom emails sent via the
mailservice
No additional configuration required.
Liquid Mail Templates
Directus core uses Liquid templates for transactional emails (e.g. password-reset.liquid, user-invitation.liquid). When an email is sent with template instead of html, this extension renders the template before delivery and injects the same defaults Directus provides:
| Variable | Source |
| ----------------- | --------------------------------------------------- |
| projectName | directus_settings.project_name (fallback Directus) |
| projectColor | directus_settings.project_color (fallback #171717) |
| projectLogo | directus_settings.project_logo resolved against PUBLIC_URL, otherwise admin/img/directus-white.png |
| projectUrl | directus_settings.project_url |
Template lookup order:
EMAIL_TEMPLATES_PATH(if set and the file exists)- The
dist/services/mail/templatesfolder shipped with@directus/api/directus- Resolved via
require.resolve - Falls back to common locations (
/directus/node_modules/...,process.cwd()/node_modules/...) - Also scans
node_modules/.pnpm/@directus+api@*for pnpm-based installs
- Resolved via
Drop your custom .liquid files into EMAIL_TEMPLATES_PATH to override or add templates.
Manual Email Sending (Flow Operation)
Use the Microsoft Graph: Send Email operation in Directus Flows.
Operation Settings:
- To: Recipient email(s) — string or array
- CC: CC recipients (optional)
- BCC: BCC recipients (optional)
- Subject: Email subject
- Body: Email body (HTML or plain text)
- Type:
htmlortext(default:html) - Importance:
low,normal, orhigh(default:normal) - Save to Sent Items: Boolean (default:
true)
Example Flow payload:
{
"to": "[email protected]",
"subject": "Welcome to our platform",
"body": "<h1>Welcome!</h1><p>Thank you for signing up.</p>",
"type": "html",
"importance": "normal"
}Troubleshooting
Extension not loading
Check Directus logs for errors:
docker logs directus_backendEmails not being sent
Check environment variables:
docker exec directus_backend env | grep MSGRAPHVerify Azure AD permissions:
- Ensure
Mail.Sendpermission is granted - Ensure admin consent is given
- Ensure
Check sender email:
- The sender email must be a valid mailbox in your Microsoft 365 tenant
- The App Registration needs permission to send as this user
Check logs: Look for
[msgraph-mail]entries in Directus logs
Template-based emails (password reset, invitations) fail
If you see an error like "Could not locate built-in Directus mail templates" in the logs:
- Make sure the extension can reach
@directus/api'sdist/services/mail/templatesfolder. In Docker images this is usually/directus/node_modules/@directus/api/dist/services/mail/templates. - As a workaround you can set
EMAIL_TEMPLATES_PATHto a folder containing copies of the required.liquidfiles (e.g.password-reset.liquid,user-invitation.liquid).
Token acquisition fails
- Verify
MSGRAPH_TENANT_ID,MSGRAPH_CLIENT_ID, andMSGRAPH_CLIENT_SECRETare correct - Ensure the client secret has not expired
- Check Azure AD App Registration is not disabled
How it Works
Hook (Automatic Email Interception)
The extension registers an email.send filter hook that intercepts all outgoing emails. If a template is provided (and no html), the template is rendered with LiquidJS using project defaults plus the custom data from the caller. The result is then sent via Microsoft Graph API instead of the default SMTP transport.
Operation (Manual Email Sending)
The extension provides a Directus Flow operation that can be used to send emails programmatically from your flows.
Token Management
The extension automatically acquires and caches OAuth2 access tokens. Tokens are refreshed automatically when they expire (with a 5-minute buffer).
Requirements
- Directus
^10.10.0or^11.0.0 - Microsoft 365 / Azure AD tenant
- Azure AD App Registration with
Mail.Sendpermission - Non-sandboxed extension execution (default for self-hosted Directus instances)
License
MIT License — see LICENSE file for details
Contributing
Contributions are welcome. Please open an issue or submit a pull request on GitHub.
Support
Changelog
1.0.5
- Added official compatibility with Directus 11 (host range now
^10.10.0 || ^11.0.0) - Bumped
@directus/extensions-sdkto^17.0.0 - Verified that the template auto-discovery still resolves
@directus/api'sdist/services/mail/templatesfolder under Directus 11 (@directus/[email protected])
1.0.4
- Switched HTTP transport from sandboxed
directus:apirequestto nativefetch - Added Liquid mail template rendering with the same project defaults Directus uses (
projectName,projectLogo,projectColor,projectUrl) - Auto-discovery of Directus' built-in templates folder, with optional
EMAIL_TEMPLATES_PATHoverride - Sandbox mode disabled to allow filesystem access for templates
- Added
liquidjsruntime dependency
Author
CodeCrush
- Website: codecrush.ch
- GitHub: @codecrush-ch
Made with ❤️ by CodeCrush
