Skip to content

Commit

Permalink
feat: complete support for resend and SMTP transports (#27)
Browse files Browse the repository at this point in the history
* chore: add Mailtrap logo to README

* chore: remove config to check JS

* chore: remove index.d.ts

* feat: add Mailtrap support

* chore: update readme

* feat: add complete option for Resend transport

* feat: add headers option

* chore: update punctuation
  • Loading branch information
DominusKelvin authored Nov 20, 2024
1 parent 87c1639 commit 94e7925
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 59 deletions.
30 changes: 6 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,16 @@ module.exports.mail = {
}
```

## log
## Supported transports

To use the `log` mailer, set the `default` property of `config/mail.js` to `log` and make sure you have a `log` mailer under the `mailers` object.

Sails will log your email to the console

## SMTP

To use SMTP as a mailer set it as the `default` in `config/mail.js` and also install the peer dependency `nodemailer`:

```sh
npm i nodemailer --save
```

Then in your `config/local.js` you can provide the SMTP credentials like so:

```js
// config/local.js
smtp: {
host: 'HOST',
username: 'USERNAME',
password: 'PASSWORD'
}
```
- [Log](https://docs.sailscasts.com/mail/local-development#log-transport)
- [SMTP](https://docs.sailscasts.com/mail/smtp-transport)
- [Mailtrap](https://docs.sailscasts.com/mail/mailtrap-transport)
- [Resend](https://docs.sailscasts.com/mail/resend-transport)

## Email Partners

A big thank you to all our partners for their contributions and efforts in making Sails Mail development possible!
A big thank you to all our partners for their contributions and efforts in making Sails Mail development possible.

<div style="display: flex; gap: 30px;">
<a href="https://mailtrap.io?utm_source=sails-hook-mail">
Expand Down
100 changes: 65 additions & 35 deletions lib/private/mail/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,6 @@ module.exports = {
required: true,
isEmail: true
},
cc: {
type: 'ref',
description:
'Comma separated list or an array of recipients email addresses that will appear on the Cc: field',
defaultsTo: []
},
bcc: {
type: 'ref',
description:
'Comma separated list or an array of recipients email addresses that will appear on the Bcc: field',
defaultsTo: []
},
toName: {
description: 'Name of the primary recipient as displayed in their inbox.',
example: 'Nola Thacker'
Expand All @@ -88,13 +76,6 @@ module.exports = {
example: 'Anne Martin',
defaultsTo: sails.config.mail.from.name || process.env.MAIL_FROM_NAME
},

replyTo: {
description: 'An email address that will appear on the Reply-To: field',
example: '[email protected]',
defaultsTo: sails.config.mail.replyTo || process.env.MAIL_REPLY_TO
},

layout: {
description:
'Set to `false` to disable layouts altogether, or provide the path (relative ' +
Expand Down Expand Up @@ -134,6 +115,40 @@ module.exports = {
type: 'string',
description: 'The ID of the test inbox to use.',
defaultsTo: process.env.MAILTRAP_TEST_INBOX_ID
},
// SMTP and Resend transport specific options
replyTo: {
description: 'An email address that will appear on the Reply-To: field',
example: '[email protected]',
defaultsTo: sails.config.mail.replyTo || process.env.MAIL_REPLY_TO
},
cc: {
type: 'ref',
description:
'Comma separated list or an array of recipients email addresses that will appear on the Cc: field',
defaultsTo: []
},
bcc: {
type: 'ref',
description:
'Comma separated list or an array of recipients email addresses that will appear on the Bcc: field',
defaultsTo: []
},
headers: {
type: 'ref',
description: 'An object of additional headers to include in the email.',
default: {}
},
// Resend transport specific options
react: {
type: 'string',
description: 'The React component used to write the message.'
},
scheduledAt: {
type: 'string',
description: 'Schedule email to be sent later.',
extendDescription:
'The date should be in language natural (e.g.: in 1 min) or ISO 8601 format (e.g: 2024-08-05T11:52:01.858Z).'
}
},

Expand All @@ -144,25 +159,30 @@ module.exports = {
},

fn: async function ({
mailer,
template,
templateData,
layout,
to,
subject,
mailer,
from: fromAddress,
fromName,
replyTo,
text,
attachments,
// SMTP and Resend transport specific options
replyTo,
cc,
bcc,
attachments,
headers,
// Mailtrap transport specific options
templateUuid,
templateVariables,
category,
customVariables,
testInboxId
testInboxId,
// Resend transport specific options
react,
scheduledAt
}) {
if (template && !template.startsWith('email-')) {
sails.log.warn(
Expand Down Expand Up @@ -239,8 +259,7 @@ module.exports = {
process.env.MAIL_PASSWORD
}
})

const smtpInfo = await transporter.sendMail({
const smtpMail = {
from: {
name: fromName,
address: fromAddress
Expand All @@ -252,8 +271,10 @@ module.exports = {
cc,
bcc,
replyTo,
attachments
})
attachments,
headers
}
const smtpInfo = await transporter.sendMail({ ...smtpMail })
sails.log.debug('Email sent: %s', smtpInfo.messageId)
break
case 'mailtrap':
Expand All @@ -271,7 +292,7 @@ module.exports = {
process.env.MAILTRAP_ACCOUNT_ID
}
const mailtrap = new MailtrapClient({ ...mailtrapClientOptions })
const mail = {
const mailtrapMail = {
category,
from: { name: fromName, email: fromAddress },
to: [{ email: to }],
Expand All @@ -283,14 +304,14 @@ module.exports = {
template_variables: templateVariables,
custom_variables: customVariables
}

let mailtrapInfo
if (testInboxId) {
mailtrapInfo = await mailtrap.testing.send({ ...mail })
mailtrapInfo = await mailtrap.testing.send({ ...mailtrapMail })
} else {
mailtrapInfo = await mailtrap.send({ ...mail })
mailtrapInfo = await mailtrap.send({ ...mailtrapMail })
}

mailtrapInfo = await mailtrap.send({ ...mail })

sails.log.debug('Email sent: %s', mailtrapInfo.message_ids.join(', '))
break
case 'resend':
Expand All @@ -299,12 +320,21 @@ module.exports = {
sails.config.mail.mailers[mailer]?.apiKey ||
process.env.RESEND_API_KEY
const resend = new Resend(apiKey)
const resendInfo = await resend.emails.send({
const resendMail = {
from: `${fromName} <${fromAddress}>`,
replyTo,
to,
subject,
html
})
html,
text,
cc,
bcc,
attachments,
react,
scheduledAt,
headers
}
const resendInfo = await resend.emails.send({ ...resendMail })
sails.log.debug('Email sent: %s', resendInfo.id)
break
case 'log':
Expand Down

0 comments on commit 94e7925

Please sign in to comment.