You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
4 years ago
|
import nodemailer from 'nodemailer';
|
||
|
|
||
5 years ago
|
const { SendMailError } = require('../error');
|
||
5 years ago
|
|
||
4 years ago
|
/**
|
||
|
* @param {string} to
|
||
|
* @param {string} subject
|
||
|
* @param {string} text
|
||
|
* @param {string} html
|
||
|
*/
|
||
4 years ago
|
// eslint-disable-next-line import/prefer-default-export
|
||
|
export async function sendMail(to: string, subject: string, text: string, html: string) {
|
||
|
let t: unknown;
|
||
4 years ago
|
try {
|
||
|
const { system } = global.Hydro.model;
|
||
|
const [host, port, secure, user, pass, from] = await Promise.all([
|
||
|
system.get('smtp.host'),
|
||
|
system.get('smtp.port'),
|
||
|
system.get('smtp.secure'),
|
||
|
system.get('smtp.user'),
|
||
|
system.get('smtp.pass'),
|
||
|
system.get('smtp.from'),
|
||
|
]);
|
||
|
const transporter = nodemailer.createTransport({
|
||
|
host, port, secure, auth: { user, pass },
|
||
|
});
|
||
|
t = await new Promise((resolve, reject) => {
|
||
|
transporter.sendMail({
|
||
|
from,
|
||
|
to,
|
||
|
subject,
|
||
|
text,
|
||
|
html,
|
||
|
}, (err, info) => {
|
||
|
if (err) reject(err);
|
||
|
else resolve(info);
|
||
4 years ago
|
});
|
||
4 years ago
|
});
|
||
|
} catch (e) {
|
||
|
throw new SendMailError(to);
|
||
|
}
|
||
|
return t;
|
||
|
}
|
||
|
|
||
4 years ago
|
global.Hydro.lib.mail = {
|
||
4 years ago
|
sendMail,
|
||
5 years ago
|
};
|