Send Helper
Mail provides a send
helper that is used to send your emails within your Sails applications. The send
helper takes in several optional and required arguments like mailer
, template
, templateData
etc.
mailer
The mailer to use for sending the email. This is optional because by default Mail will look for the mailer to use by checking for it in the following places:
- an environment variable called
MAIL_MAILER
, - a
default
property in amail
object inconfig/local.js
, - and finally the
default
mailer set inconfig/mail.js
sails.helpers.mail.send.with({ mailer: 'resend' })
template
This is a string matching an email template in views/emails
without the file extension.
sails.helpers.mail.send.with({ template: 'email-verify-account' })
templateData
A dictionary of data which will be accessible in your email template.
sails.helpers.mail.send.with({ templateData: { fullName: 'Jack Sparrow' } })
to
required
The email address of the primary recipient.
sails.helpers.mail.send.with({ to: 'jack@blackpearl.com' })
toName
The name of the primary recipient
sails.helpers.mail.send.with({ toName: 'Jack Sparrow' })
cc
SMTP Resend
The email addresses to send a carbon copy of the mail to.
sails.helpers.mail.send.with({ cc: ['jack@blackpearl.com'] })
bcc
SMTP Resend
The email addresses to send a blind carbon copy of the mail to.
sails.helpers.mail.send.with({ cc: ['jack@blackpearl.com'] })
subject
The subject of the email.
sails.helpers.mail.send.with({ subject: 'Verify email' })
from
The from email to use to send the email. This isn't required because you can specify a global from
sails.helpers.mail.send.with({ from: 'boring@sailscasts.com' })
fromName
The from name to use to send the email. This isn't required because you can specify a global from
sails.helpers.mail.send.with({ fromName: 'The Boring JavaScript Stack' })
layout
The email layout to use for this email. This isn't required because by default Mail will look for a layout called layout-email
.
However with layout
you can pass an override and chose another layout to use or you can disable layout by passing false
Specify a layout
await sails.helpers.mail.send.with({ layout: 'layout-account' })
Disable layout
await sails.helpers.mail.send.with({ layout: false })
text
Specify email plain text.
await sails.helpers.mail.send.with({ text: 'Verify your email' })
attachments
An optional array of attachment objects. Each attachment object should have the following properties:
filename
: The name of the file.path
: The path to the file.contentType
: The MIME type of the file (optional).content
: String, Buffer or a Stream contents for the attachment (optional).
await sails.helpers.mail.send.with({
to: 'jack@blackpearl.com',
toName: 'Jack Sparrow',
template: 'email-verify-account',
templateData: { token: '3828bsbababvbas', fullName: 'Jack Sparrow' },
attachments: [
{
filename: 'adventure-log.txt',
content: 'The journey begins...'
},
{
filename: 'treasure-map.txt',
content: new Buffer('X marks the spot!', 'utf-8')
},
{
filename: 'crew-list.txt',
path: '/path/to/crew-list.txt'
},
{
filename: 'ship-photo.jpg',
path: '/path/to/ship-photo.jpg',
contentType: 'image/jpeg'
}
]
})
replyTo
SMTP Resend
An email address that will appear on the Reply-To: field. This isn't required because you can specify a global replyTo.
sails.helpers.mail.send.with({ replyTo: 'support@sailscasts.com' })
category
Mailtrap
The category of the email.
await sails.helpers.mail.send.with({
category: 'verify'
})
customVariables
Mailtrap
An object with custom variables to use in the email.
await sails.helpers.mail.send.with({
customVariables: {
shipName: 'Black Pearl',
captain: 'Jack Sparrow'
}
})
templateUuid
Mailtrap
The UUID of the template to use.
await sails.helpers.mail.send.with({
templateUuid: '123e4567-e89b-12d3-a456-426614174000'
})
templateVariables
Mailtrap
An object with the variables to use in the template.
await sails.helpers.mail.send.with({
templateUuid: '123e4567-e89b-12d3-a456-426614174000',
templateVariables: {
shipName: 'Black Pearl',
captain: 'Jack Sparrow'
}
})
testInboxId
Mailtrap
The ID of the test inbox to use.
await sails.helpers.mail.send.with({
to: 'jack@blackpearl.com',
toName: 'Jack Sparrow',
template: 'email-verify-account',
templateData: { token: '3828bsbababvbas', fullName: 'Jack Sparrow' },
testInboxId: 5353939
})
react
Resend
If you are using React Email, you can pass the the React component you used in writing the message to react
.
import * as React from 'react'
import { Html, Button, Container, Heading, Text } from '@react-email/components'
export default function Email(props) {
const { url, recipientName } = props
return (
<Html lang="en">
<Container>
<Heading>Ahoy, {recipientName}!</Heading>
<Text>We have an important message for you. Click the button below to read it:</Text>
<Button href={url}>Read Message</Button>
<Text>Fair winds and following seas,</Text>
<Text>The Black Pearl Crew</Text>
</Container>
</Html>
)
}
const Email = require('../../assets/emails/important-message')
await sails.helpers.mail.send.with({
react: <Email url="https://example.com" recipientName="Jack Sparrow" />,
to: 'jack@blackpearl.com',
toName: 'Jack Sparrow'
})
TIP
Make sure you have @react-email/components
installed to use React components as email
scheduledAt
Resend
Schedule email to be sent later. The date should be in natural language (e.g.: in 1 min) or ISO 8601 format (e.g: 2024-08-05T11:52:01.858Z).
await sails.helpers.mail.send.with({
to: 'jack@blackpearl.com',
toName: 'Jack Sparrow',
template: 'email-verify-account',
templateData: { token: '3828bsbababvbas', fullName: 'Jack Sparrow' },
scheduledAt: '2024-08-05T11:52:01.858Z'
})
Examples
Send email with template
await sails.helpers.mail.send.with({
to: 'jack@blackpearl.com',
toName: 'Jack Sparrow',
template: 'email-verify-account',
templateData: { token: '3828bsbababvbas', fullName: 'Jack Sparrow' }
})
Disable layout
await sails.helpers.mail.send.with({
to: 'jack@blackpearl.com',
toName: 'Jack Sparrow',
template: 'email-verify-account',
templateData: { token: '3828bsbababvbas', fullName: 'Jack Sparrow' },
layout: false
})
Send plain text email
await sails.helpers.mail.send.with({
to: 'jack@blackpearl.com',
toName: 'Jack Sparrow',
text: 'Hello Jack, how is the black pearl'
})