12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- import config from "config";
- import nodemailer from "nodemailer";
- import CoreClass from "../../core";
- let MailModule;
- class _MailModule extends CoreClass {
-
- constructor() {
- super("mail");
- MailModule = this;
- }
-
- async initialize() {
- const importSchema = schemaName =>
- new Promise(resolve => {
- import(`./schemas/${schemaName}`).then(schema => resolve(schema.default));
- });
- this.schemas = {
- verifyEmail: await importSchema("verifyEmail"),
- resetPasswordRequest: await importSchema("resetPasswordRequest"),
- passwordRequest: await importSchema("passwordRequest"),
- dataRequest: await importSchema("dataRequest")
- };
- this.enabled = config.get("mail.enabled");
- if (this.enabled) this.transporter = nodemailer.createTransport(config.get("mail.smtp"));
- return new Promise(resolve => {
- resolve();
- });
- }
-
- SEND_MAIL(payload) {
- return new Promise((resolve, reject) => {
- if (MailModule.enabled) {
- const { data } = payload;
- if (!data.from)
- data.from =
- config.get("mail.from").length > 0
- ? config.get("mail.from")
- : `${config.get("sitename")} <noreply@${config.get("url.host")}>`;
- MailModule.transporter
- .sendMail(payload.data)
- .then(info => {
- MailModule.log("SUCCESS", "MAIL_SEND", `Successfully sent email ${info.messageId}`);
- return resolve();
- })
- .catch(err => {
- MailModule.log("ERROR", "MAIL_SEND", `Failed to send email. ${err}`);
- return reject();
- });
- return;
- }
- resolve();
- });
- }
-
- GET_SCHEMA(payload) {
- return new Promise(resolve => {
- resolve(MailModule.schemas[payload.schemaName]);
- });
- }
- }
- export default new _MailModule();
|