72 lines
2.2 KiB
JavaScript
72 lines
2.2 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const tslib_1 = require("tslib");
|
|
//https://www.nodemailer.com/
|
|
const nodemailer_1 = tslib_1.__importDefault(require("nodemailer"));
|
|
class Nodemailer {
|
|
name = 'nodemailer';
|
|
async sendEmail(options, context) {
|
|
const { host, port, account, password, subject, from, text, html, to, attachments } = options;
|
|
const transporter = nodemailer_1.default.createTransport({
|
|
host,
|
|
port,
|
|
secure: port === 465,
|
|
auth: {
|
|
user: account,
|
|
pass: password,
|
|
},
|
|
});
|
|
async function verifyTransporter() {
|
|
return new Promise((resolve, reject) => {
|
|
transporter.verify((error, success) => {
|
|
if (error) {
|
|
reject(error);
|
|
}
|
|
else {
|
|
resolve(success);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
try {
|
|
await verifyTransporter();
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
console.log('Server is ready to take our messages');
|
|
}
|
|
let mailOptions = {
|
|
from,
|
|
to,
|
|
subject,
|
|
text,
|
|
html,
|
|
attachments: attachments
|
|
};
|
|
try {
|
|
let info = await transporter.sendMail(mailOptions);
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
console.log('info', info);
|
|
console.log('Message sent: %s', info.messageId);
|
|
}
|
|
return {
|
|
success: true,
|
|
};
|
|
}
|
|
catch (err) {
|
|
console.log('sendMailError', err);
|
|
return {
|
|
success: false,
|
|
error: err?.message,
|
|
};
|
|
}
|
|
}
|
|
catch (error) {
|
|
console.error('verifyError', error);
|
|
return {
|
|
success: false,
|
|
error: error?.message,
|
|
};
|
|
}
|
|
}
|
|
}
|
|
exports.default = Nodemailer;
|