mirror of
https://github.com/matt-fidd/stratos.git
synced 2026-01-01 17:59:25 +00:00
Added functionality to Emailer to create nodemailer transport in preparation for email sending
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
// Import required modules
|
||||
const nodemailer = require('nodemailer');
|
||||
|
||||
// Import user defined modules
|
||||
const importJSON = require('./importJSON');
|
||||
|
||||
/**
|
||||
@@ -198,6 +202,12 @@ class Emailer {
|
||||
*/
|
||||
#secure = true;
|
||||
|
||||
/**
|
||||
* Represents the nodemailer transporter object
|
||||
* @type {Object}
|
||||
*/
|
||||
#transporter;
|
||||
|
||||
/**
|
||||
* @param {Object} [connectionOpts] - The SMTP connection details
|
||||
* @param {string} connectionOpts.host - The host address of the server
|
||||
@@ -221,6 +231,9 @@ class Emailer {
|
||||
* @return {boolean}
|
||||
*/
|
||||
async sendEmail(email) {
|
||||
if (typeof this.#transporter === 'undefined')
|
||||
await this.getTransporter();
|
||||
|
||||
console.log(`Sending`);
|
||||
console.log(email);
|
||||
console.log(this.#host,
|
||||
@@ -229,6 +242,37 @@ class Emailer {
|
||||
this.#secure);
|
||||
}
|
||||
|
||||
/**
|
||||
* getTransportOptions() Builds the options object for nodemailer
|
||||
*
|
||||
* @return {Object}
|
||||
*/
|
||||
getTransportOptions() {
|
||||
return {
|
||||
host: this.#host,
|
||||
secure: this.#secure,
|
||||
auth: {
|
||||
user: this.#user,
|
||||
pass: this.#password
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* getTransporter() Gets a nodemailer transporter with current options
|
||||
*/
|
||||
async getTransporter() {
|
||||
this.#transporter =
|
||||
nodemailer.createTransport(this.getTransportOptions());
|
||||
|
||||
try {
|
||||
await this.#transporter.verify();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
throw new Error('Unable to connect');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets if the SMTP connection should be encrypted or not
|
||||
* @param {boolean} val - The new value
|
||||
@@ -238,6 +282,7 @@ class Emailer {
|
||||
throw new Error('Secure must be type boolean');
|
||||
|
||||
this.#secure = val;
|
||||
this.#transporter = this.getTransporter();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user