Skip to content

Commit

Permalink
feat: add Mailtrap support
Browse files Browse the repository at this point in the history
  • Loading branch information
DominusKelvin committed Nov 20, 2024
1 parent b207764 commit 6a79b63
Showing 1 changed file with 69 additions and 3 deletions.
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

0 comments on commit 6a79b63

Please sign in to comment.