Send email messages with Node.js
This page explains how to use the Nylas Node.js SDK and Email API to create a draft and send it as an email message. For more information, see the Email documentation.
Before you begin
Before you start, you must have done the following tasks:
Create and send a draft
To create a draft and send it as an email message, first create a Draft object and assign it attributes.
import Nylas from 'nylas'
const draft = {
subject: "With Love, from Nylas",
to: [{ name: "My Nylas Friend", email: "devrel@nylas.com" }],
body: "This email was sent using the Nylas Email API. Visit https://nylas.com for details.",
}
const createdDraft = await nylas.drafts.create({
// Identifier is the grant ID received when connecting a user.
identifier,
requestBody: draft
})
The to
parameter is an array of email objects that contains names and email addresses. You can use to
for cc
and bcc
.
The following example creates a draft email message called "With Love, from Nylas" and sends it to a single recipient.
import 'dotenv/config'
import Nylas from 'nylas'
const NylasConfig = {
apiKey: '<NYLAS_API_KEY>',
apiUri: '<NYLAS_API_URI>',
}
const nylas = new Nylas(NylasConfig)
const identifier = '<NYLAS_GRANT_ID>'
const createAndSendDraft = async () => {
const draft = {
subject: "With Love, from Nylas",
to: [{ name: "My Nylas Friend", email: "devrel@nylas.com" }],
body: "This email was sent using the Nylas Email API. Visit https://nylas.com for details.",
}
const { id: draftId } = await nylas.drafts.create({
identifier,
requestBody: draft
})
const message = await nylas.drafts.send({ identifier, draftId })
}
createAndSendDraft()
Explore the Email API
If you've made it to this point, congratulations! You've learned how to send email messages with the Nylas Node.js SDK and Email API! 🎉
There's plenty more that you can do with Nylas. Take a look at the following resources to learn more: