From 3177653d60adc0831b9fe5c91715aa6afa5ae7a0 Mon Sep 17 00:00:00 2001 From: matt Date: Mon, 14 Feb 2022 17:27:41 +0000 Subject: [PATCH] Added Emailer class to send emails --- lib/Emailer.js | 76 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/lib/Emailer.js b/lib/Emailer.js index 82e8744..a8841e9 100644 --- a/lib/Emailer.js +++ b/lib/Emailer.js @@ -1,5 +1,7 @@ 'use strict'; +const importJSON = require('./importJSON'); + /** * removeDupes() Removes item from src array that appear in dest * @@ -168,6 +170,78 @@ class EmailBuilder { } } +/** + * Allows for email sending + */ +class Emailer { + /** + * The host address for the SMTP server to send from + * @type {string} + */ + #host; + + /** + * The username to autheniticate against the SMTP server with + * @type {string} + */ + #user; + + /** + * The password to autheniticate against the SMTP server with + * @type {string} + */ + #password; + + /** + * Whether to use an encrypted connection or not + * @type {boolean} + */ + #secure = true; + + /** + * @param {Object} [connectionOpts] - The SMTP connection details + * @param {string} connectionOpts.host - The host address of the server + * @param {string} connectionOpts.user - The SMTP user + * @param {string} connectionOpts.password - The SMTP password + */ + constructor(connectionOpts) { + if (typeof connectionOpts === 'undefined') + connectionOpts = importJSON('email'); + + this.#host = connectionOpts.host; + this.#user = connectionOpts.user; + this.#password = connectionOpts.password; + } + + /** + * sendEmail() Send an EmailBuilder object as an email + * + * @param {EmailBuilder} email - The email object to send + * + * @return {boolean} + */ + async sendEmail(email) { + console.log(`Sending`); + console.log(email); + console.log(this.#host, + this.#user, + this.#password, + this.#secure); + } + + /** + * Sets if the SMTP connection should be encrypted or not + * @param {boolean} val - The new value + */ + set secure(val) { + if (typeof val !== 'boolean') + throw new Error('Secure must be type boolean'); + + this.#secure = val; + } +} + module.exports = { - EmailBuilder: EmailBuilder + EmailBuilder: EmailBuilder, + Emailer: Emailer };