Nodemailer transport using microsoft 365 graph api
npm install nodemailer-msgraph-transportThis module is a transport plugin for Nodemailer
that use Microsoft Graph API
to send e-mails from a tenant.
#### Create a secret
1. Go to Manage -> Certificates and secrets
2. New client secret, give it a name
3. Keep the client secret value
#### Add graph permissions
1. Go to Manage -> API permissions
2. Add a permission
3. Select Microsoft Graph then Application permissions
4. Search for Mail.Send, select it and click on Add permissions
5. Click on Grant admin consent for "your tenant"
npm install nodemailer-msgraph-transport
Require the module and intialize it with the Microsoft application credentials
``javascript
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.
`javascript
await msGraphTransport.getAccessToken()
transporter.sendMail(mailData)
transporter.sendMail(mailData)
...
``