vmagic
Version:
vMagic is a RESTFul framework for NodeJS applications.
94 lines (79 loc) • 2.73 kB
JavaScript
/*eslint max-statements: ["error", 30]*/
"use strict";
import { createTransport } from "nodemailer";
import AppUtil from "../Util/AppUtil.js";
//BUG error to attempt to send email. The configuration is undefined.
class Email {
constructor() {
this.init();
}
async init() {
const corePath = `${AppUtil.getAppStructure().configPath}/core.json`;
const core = await AppUtil.importStaticFile(corePath);
this.mConfig = core.email;
}
/**
* [send description]
* @param {[type]} data [description]
* @param {Function} callback [description]
* @return {[type]} [description]
*/
send(data) {
this.data = data;
const options = {};
return new Promise((resolve, reject) => {
//Validation Fields
if (!this.mConfig.username) {
reject(new Error("username cannot be null."));
return;
} else if (!this.mConfig.password) {
reject(new Error("password cannot be null."));
return;
} else if (!this.data.to) {
reject(new Error("to cannot be null."));
return;
} else if (!this.data.subject) {
reject(new Error("subject cannot be null."));
return;
}
if (this.mConfig.host) {
options.host = this.mConfig.host;
}
if (this.mConfig.port) {
options.port = this.mConfig.port;
}
if (this.mConfig.secure) {
options.secure = this.mConfig.secure; //True for 465, false for other ports
}
if (this.mConfig.tls) {
options.tls = this.mConfig.tls;
}
options.auth = {
user: this.mConfig.username,
pass: this.mConfig.password,
};
const smtpTransport = createTransport(options);
const mail = {
from: this.mConfig.from,
to: this.data.to,
subject: this.data.subject,
text: this.data.text,
html: this.data.html,
};
//If exists files attachments
if (this.data.attachments) {
//Params, filename, path
mail.attachments = this.data.attachments;
}
smtpTransport.sendMail(mail, (err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
smtpTransport.close();
});
});
}
}
export default Email;