-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: complete support for resend and SMTP transports (#27)
* 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
1 parent
87c1639
commit 94e7925
Showing
2 changed files
with
71 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' | ||
|
@@ -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 ' + | ||
|
@@ -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).' | ||
} | ||
}, | ||
|
||
|
@@ -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( | ||
|
@@ -239,8 +259,7 @@ module.exports = { | |
process.env.MAIL_PASSWORD | ||
} | ||
}) | ||
|
||
const smtpInfo = await transporter.sendMail({ | ||
const smtpMail = { | ||
from: { | ||
name: fromName, | ||
address: fromAddress | ||
|
@@ -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': | ||
|
@@ -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 }], | ||
|
@@ -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': | ||
|
@@ -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': | ||
|