-
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.
- Loading branch information
Showing
3 changed files
with
75 additions
and
56 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 |
---|---|---|
|
@@ -9,7 +9,7 @@ module.exports = { | |
description: 'The mailer to used.', | ||
extendedDescription: | ||
'The mailer should be configured properly in config/mails.js. If not specified, the default mailer in sails.config.mail.default will be used', | ||
defaultsTo: sails.config.mail.default | ||
defaultsTo: sails.config.mail.default || process.env.MAIL_MAILER | ||
}, | ||
template: { | ||
description: | ||
|
@@ -52,25 +52,23 @@ module.exports = { | |
description: 'Name of the primary recipient as displayed in their inbox.', | ||
example: 'Nola Thacker' | ||
}, | ||
|
||
subject: { | ||
description: 'The subject of the email.', | ||
example: 'Hello there.', | ||
defaultsTo: '' | ||
}, | ||
|
||
from: { | ||
description: | ||
'An override for the default "from" email that\'s been configured.', | ||
example: '[email protected]', | ||
isEmail: true, | ||
defaultsTo: sails.config.mail.from.address | ||
defaultsTo: sails.config.mail.from.address || process.env.MAIL_FROM_ADRESS | ||
}, | ||
|
||
fromName: { | ||
description: 'An override for the default "from" name.', | ||
example: 'Anne Martin', | ||
defaultsTo: sails.config.mail.from.name | ||
defaultsTo: sails.config.mail.from.name || process.env.MAIL_FROM_NAME | ||
}, | ||
|
||
layout: { | ||
|
@@ -164,55 +162,77 @@ module.exports = { | |
err.message | ||
return err | ||
}) | ||
switch (sails.config.mail.mailers[mailer].transport) { | ||
case 'smtp': | ||
const nodemailer = getModule('nodemailer') | ||
var transporter = nodemailer.createTransport({ | ||
host: | ||
process.env.MAIL_HOST || | ||
sails.config[mailer].host || | ||
sails.config.mail.mailers[mailer].host, | ||
port: | ||
process.env.MAIL_PORT || | ||
sails.config[mailer].port || | ||
sails.config.mail.mailers[mailer].port, | ||
secure: | ||
process.env.MAIL_SECURE || | ||
sails.config[mailer].secure || | ||
sails.config.mail.mailers[mailer].secure || | ||
false, | ||
auth: { | ||
user: | ||
process.env.MAIL_USERNAME || | ||
sails.config[mailer].username || | ||
sails.config.mail.mailers[mailer].username, | ||
pass: | ||
process.env.MAIL_PASSWORD || | ||
sails.config[mailer].password || | ||
sails.config.mail.mailers[mailer].password | ||
} | ||
}) | ||
|
||
if ( | ||
mailer == 'log' || | ||
sails.config.mail.mailers[mailer].transport == 'log' | ||
) { | ||
const logMessage = ` | ||
Mailer is set to log so Sails is logging the email: | ||
-=-=-=-=-=-=-=-=-=-=-=-=-= Email log -=-=-=-=-=-=-=-=-=-=-=-=-= | ||
To: ${to} | ||
Subject: ${subject} | ||
const smtpInfo = await transporter.sendMail({ | ||
from: { | ||
name: fromName, | ||
address: fromAddress | ||
}, | ||
to, | ||
subject, | ||
text, | ||
html | ||
}) | ||
sails.log.debug('Message sent: %s', smtpInfo.messageId) | ||
break | ||
case 'resend': | ||
const { Resend } = getModule('resend') | ||
const apiKey = | ||
process.env.RESEND_API_KEY || | ||
sails.config[mailer].apiKey || | ||
sails.config.mail.mailers[mailer].apiKey | ||
const resend = new Resend(apiKey) | ||
const resendInfo = await resend.emails.send({ | ||
from: `${fromName} <${fromAddress}>`, | ||
to, | ||
subject, | ||
html | ||
}) | ||
sails.log.debug('Message sent: %s', resendInfo.id) | ||
break | ||
case 'log': | ||
const logMessage = ` | ||
Mailer is set to log so Sails is logging the email: | ||
-=-=-=-=-=-=-=-=-=-=-=-=-= Email log -=-=-=-=-=-=-=-=-=-=-=-=-= | ||
To: ${to} | ||
Subject: ${subject} | ||
Body: | ||
${html} | ||
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
` | ||
sails.log(logMessage) | ||
} else if ( | ||
mailer == 'smtp' || | ||
sails.config.mail.mailers[mailer].transport == 'smtp' | ||
) { | ||
const nodemailer = getModule('nodemailer') | ||
var transporter = nodemailer.createTransport({ | ||
host: | ||
sails.config[mailer].host || sails.config.mail.mailers[mailer].host, | ||
port: | ||
sails.config[mailer].port || sails.config.mail.mailers[mailer].port, | ||
auth: { | ||
user: | ||
sails.config[mailer].username || | ||
sails.config.mail.mailers[mailer].username, | ||
pass: | ||
sails.config[mailer].password || | ||
sails.config.mail.mailers[mailer].password | ||
} | ||
}) | ||
|
||
const info = await transporter.sendMail({ | ||
from: { | ||
name: fromName, | ||
address: fromAddress | ||
}, | ||
to, | ||
subject, | ||
text, | ||
html | ||
}) | ||
sails.log.debug('Message sent: %s', info.messageId) | ||
} else { | ||
sails.log.error(`Unknown mailer: ${mailer}`) | ||
Body: | ||
${html} | ||
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
` | ||
sails.log(logMessage) | ||
break | ||
default: | ||
break | ||
} | ||
return {} | ||
} | ||
|
@@ -234,7 +254,7 @@ function getModule(moduleName) { | |
requiredModule = require(moduleName) | ||
} catch (error) { | ||
throw new Error( | ||
`"${moduleName}" is not installed. Please run "npm install ${moduleName}" to install it.` | ||
`The transport you've specified for the current mailer needs the ${moduleName} NPM package but it is not installed. Please run "npm install ${moduleName}" to install it.` | ||
) | ||
} | ||
return requiredModule | ||
|
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