fix: Cleanup and refactoring
parent
80499e68d5
commit
44eb6f9c1f
@ -0,0 +1,31 @@
|
|||||||
|
module.exports = {
|
||||||
|
inputs: {
|
||||||
|
to: {
|
||||||
|
type: 'string',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
subject: {
|
||||||
|
type: 'string',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
html: {
|
||||||
|
type: 'string',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
async fn(inputs) {
|
||||||
|
const transporter = sails.hooks.smtp.getTransporter(); // TODO: check if active?
|
||||||
|
|
||||||
|
try {
|
||||||
|
const info = await transporter.sendMail({
|
||||||
|
...inputs,
|
||||||
|
from: sails.config.custom.smtpFrom,
|
||||||
|
});
|
||||||
|
|
||||||
|
sails.log.info('Email sent: %s', info.messageId);
|
||||||
|
} catch (error) {
|
||||||
|
sails.log.error(error);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
@ -0,0 +1,35 @@
|
|||||||
|
const nodemailer = require('nodemailer');
|
||||||
|
|
||||||
|
module.exports = function smtpServiceHook(sails) {
|
||||||
|
let transporter = null;
|
||||||
|
|
||||||
|
return {
|
||||||
|
/**
|
||||||
|
* Runs when this Sails app loads/lifts.
|
||||||
|
*/
|
||||||
|
|
||||||
|
async initialize() {
|
||||||
|
if (sails.config.custom.smtpHost) {
|
||||||
|
transporter = nodemailer.createTransport({
|
||||||
|
pool: true,
|
||||||
|
host: sails.config.custom.smtpHost,
|
||||||
|
port: sails.config.custom.smtpPort,
|
||||||
|
secure: sails.config.custom.smtpSecure,
|
||||||
|
auth: sails.config.custom.smtpUser && {
|
||||||
|
user: sails.config.custom.smtpUser,
|
||||||
|
pass: sails.config.custom.smtpPassword,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
sails.log.info('SMTP hook has been loaded successfully');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
getTransporter() {
|
||||||
|
return transporter;
|
||||||
|
},
|
||||||
|
|
||||||
|
isActive() {
|
||||||
|
return transporter !== null;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue