Skip to content

Commit

Permalink
feat: support mailtrap (#24)
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
  • Loading branch information
DominusKelvin authored Nov 20, 2024
1 parent 88b41f3 commit 87c1639
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 77 deletions.
4 changes: 2 additions & 2 deletions jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"lib": ["es2016"],
// silences wrong TS error, we don't compile, we only typecheck
"outDir": "./irrelevant/unused",
"allowJs": true,
"checkJs": true,
// "allowJs": true,
// "checkJs": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
Expand Down
72 changes: 69 additions & 3 deletions lib/private/mail/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ module.exports = {
'An override for the default "from" email that\'s been configured.',
example: '[email protected]',
isEmail: true,
defaultsTo: sails.config.mail.from.address || process.env.MAIL_FROM_ADDRESS
defaultsTo:
sails.config.mail.from.address || process.env.MAIL_FROM_ADDRESS
},

fromName: {
Expand All @@ -91,7 +92,7 @@ module.exports = {
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
defaultsTo: sails.config.mail.replyTo || process.env.MAIL_REPLY_TO
},

layout: {
Expand All @@ -111,6 +112,28 @@ module.exports = {
},
text: {
example: 'Hello world'
},
// Mailtrap transport specific options
templateUuid: {
type: 'string',
description: 'The UUID of the template to use.'
},
templateVariables: {
type: 'ref',
description: 'An object with the variables to use in the template.'
},
category: {
type: 'string',
description: 'The category of the email.'
},
customVariables: {
type: 'ref',
description: 'An object with the custom variables to use in the email.'
},
testInboxId: {
type: 'string',
description: 'The ID of the test inbox to use.',
defaultsTo: process.env.MAILTRAP_TEST_INBOX_ID
}
},

Expand All @@ -133,7 +156,13 @@ module.exports = {
text,
cc,
bcc,
attachments
attachments,
// Mailtrap transport specific options
templateUuid,
templateVariables,
category,
customVariables,
testInboxId
}) {
if (template && !template.startsWith('email-')) {
sails.log.warn(
Expand Down Expand Up @@ -227,6 +256,43 @@ module.exports = {
})
sails.log.debug('Email sent: %s', smtpInfo.messageId)
break
case 'mailtrap':
const { MailtrapClient } = getModule('mailtrap')
testInboxId = testInboxId
? testInboxId
: sails.config.mail.mailers[mailer]?.testInboxId
const mailtrapClientOptions = {
token:
sails.config.mail.mailers[mailer]?.token ||
process.env.MAILTRAP_TOKEN,
testInboxId,
accountId:
sails.config.mail.mailers[mailer]?.accountId ||
process.env.MAILTRAP_ACCOUNT_ID
}
const mailtrap = new MailtrapClient({ ...mailtrapClientOptions })
const mail = {
category,
from: { name: fromName, email: fromAddress },
to: [{ email: to }],
subject,
text,
html,
attachments,
template_uuid: templateUuid,
template_variables: templateVariables,
custom_variables: customVariables
}
let mailtrapInfo
if (testInboxId) {
mailtrapInfo = await mailtrap.testing.send({ ...mail })
} else {
mailtrapInfo = await mailtrap.send({ ...mail })
}

mailtrapInfo = await mailtrap.send({ ...mail })
sails.log.debug('Email sent: %s', mailtrapInfo.message_ids.join(', '))
break
case 'resend':
const { Resend } = getModule('resend')
const apiKey =
Expand Down
72 changes: 0 additions & 72 deletions types/index.d.ts

This file was deleted.

0 comments on commit 87c1639

Please sign in to comment.