diff --git a/lib/Emailer.js b/lib/Emailer.js index 7d43311..fa6f521 100644 --- a/lib/Emailer.js +++ b/lib/Emailer.js @@ -5,6 +5,7 @@ const nodemailer = require('nodemailer'); // Import user defined modules const importJSON = require('./importJSON'); +const validator = require('./validator'); /** * removeDupes() Removes item from src array that appear in dest @@ -172,6 +173,37 @@ class EmailBuilder { this.#bcc = removeDupes(this.#bcc, addresses); return this; } + + /** + * Returns a message object for nodemailer built from message + * @returns {Object} + */ + getMessageObject() { + const message = { + from: this.#from, + to: `${this.#to.join(',')}`, + cc: `${this.#cc.join(',')}`, + bcc: `${this.#bcc.join(',')}`, + subject: this.#subject, + text: this.#body, + }; + + try { + validator.validate(message, [ + 'from', + 'subject', + 'text' + ]); + } catch (e) { + console.error(e); + return false; + } + + if (typeof this?.#HTMLBody?.length !== 'undefined') + message['html'] = this.#HTMLBody; + + return message; + } } /**