nodemailer-msgraph-transport
v1.0.1
Published
Nodemailer transport using microsoft 365 graph api
Readme
Microsoft Graph API transport for Nodemailer
This module is a transport plugin for Nodemailer that use Microsoft Graph API to send e-mails from a tenant.
Usage
Creation of an application on Microsoft Entra
- Go to Microsoft Entra admin center
- Click on App registrations then New registration
- Give a name, leave other parameters as default (single tenant, no redirect URL)
- Register the new app
- Keep the Application (Client) ID and Directory (Tenant) ID
Create a secret
- Go to Manage -> Certificates and secrets
- New client secret, give it a name
- Keep the client secret value
Add graph permissions
- Go to Manage -> API permissions
- Add a permission
- Select Microsoft Graph then Application permissions
- Search for Mail.Send, select it and click on Add permissions
- Click on Grant admin consent for "your tenant"
Install this transport
Install via npm
npm install nodemailer-msgraph-transportRequire the module and intialize it with the Microsoft application credentials
const nodemailer = require('nodemailer')
const MsGraphTransport = required('nodemailer-msgraph-transport')
const msGraphConfig = {
clientId: 'MS_GRAPH_CLIENT_ID',
tenantId: 'MS_GRAPH_TENANT_ID',
clientSecret: 'MS_GRAPH_TENANT_SECRET_VALUE',
userPrincipalName: 'id | userPrincipalName'
saveToSentItems: true
}
const msGraphTransport = new MsgraphTransport(msGraphConfig)
const transporter = nodemailer.createTransport(msGraphTransport)userPrincipalName and saveToSentItems can be overridden for each mail
in the message object passed to sendMail()
When sending a batch of e-mails, you can request a token before hand to avoid unnecessary requests to Microsoft graph api authentication server. The token will be automatically cached.
await msGraphTransport.getAccessToken()
transporter.sendMail(mailData)
transporter.sendMail(mailData)
...Limitations
Attachements are currently not supported.
References
- SendMail graph function: https://learn.microsoft.com/en-us/graph/api/user-sendmail
- graph message: https://learn.microsoft.com/en-us/graph/api/resources/message
Licence
Licensed under the MIT Licence.
