Skip to content

Commit

Permalink
Merge branch 'release/0.0.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
DominusKelvin committed Aug 8, 2023
2 parents c934451 + 628c817 commit dddc226
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 56 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# sails-hook-mail
# Mail

The simple elegant way to send emails from a Sails application

Expand Down Expand Up @@ -71,7 +71,6 @@ Then in your `config/local.js` you can provide the SMTP credentials like so:
// config/local.js
smtp: {
host: 'HOST',
encryption: 'tls',
username: 'USERNAME',
password: 'PASSWORD'
}
Expand Down
126 changes: 73 additions & 53 deletions lib/private/mail/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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 {}
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sails-hook-mail",
"version": "0.0.3",
"version": "0.0.4",
"description": "The simple elegant way to send emails from a Sails application",
"main": "lib/sails-hook-mail.js",
"scripts": {
Expand Down

0 comments on commit dddc226

Please sign in to comment.