Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate email template in cli #11

Merged
merged 7 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { program, Option } from 'commander';
import { exit } from 'process';
import ora from 'ora';
import { AUTH, CLI_COMMAND_NAME, DEFAULT_AUTH, DEFAULT_FILENAME, DEFAULT_FORMAT, DEFAULT_MIN_HEIGHT, DEFAULT_TENANT, DEFAULT_WIDTH, ENV_VAR, FORMAT, TRANSPORT_TYPE, DEFAULT_EMAIL_SUBJECT } from './constants.js';
import { AUTH, CLI_COMMAND_NAME, DEFAULT_AUTH, DEFAULT_FILENAME, DEFAULT_FORMAT, DEFAULT_MIN_HEIGHT, DEFAULT_TENANT, DEFAULT_WIDTH, ENV_VAR, FORMAT, TRANSPORT_TYPE, DEFAULT_EMAIL_SUBJECT, DEFAULT_EMAIL_NOTE } from './constants.js';
import dotenv from "dotenv";
dotenv.config();

Expand Down Expand Up @@ -56,9 +56,12 @@ export async function getCommandArguments() {
.env(ENV_VAR.SMTP_USERNAME))
.addOption(new Option('--smtppassword <password>', 'smtp password')
.env(ENV_VAR.SMTP_PASSWORD))
.addOption(new Option('--subject <subject>', 'email Subject')
.addOption(new Option('--subject <subject>', 'email subject')
.default(DEFAULT_EMAIL_SUBJECT)
.env(ENV_VAR.EMAIL_SUBJECT))
.addOption(new Option('--note <subject>', 'email note')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does email note mean email body?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, It's the note section in email body
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would users know what it means? i've not heard email note before

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does email body or message makes more sense? any other suggestion?

.default(DEFAULT_EMAIL_NOTE)
.env(ENV_VAR.EMAIL_NOTE))

program.addHelpText('after', `
Note: The tenant in the url has the higher priority than tenant value provided as command option.`);
Expand Down Expand Up @@ -90,6 +93,7 @@ function getOptions(options) {
smtppassword: null,
subject: null,
time: null,
note: null
}

// Set url.
Expand Down Expand Up @@ -145,8 +149,7 @@ function getOptions(options) {
commandOptions.filename = options.filename || process.env[ENV_VAR.FILENAME];
commandOptions.filename = options.filename === DEFAULT_FILENAME
? `${commandOptions.filename}-${commandOptions.time.toISOString()}.${commandOptions.format}`
: `${commandOptions.filename}.${commandOptions.format}`

: `${commandOptions.filename}.${commandOptions.format}`;

// Set width and height of the window
commandOptions.width = Number(options.width);
Expand All @@ -169,6 +172,9 @@ function getOptions(options) {
// Set email subject.
commandOptions.subject = options.subject || process.env[ENV_VAR.EMAIL_SUBJECT];

// Set email note.
commandOptions.note = options.note || process.env[ENV_VAR.EMAIL_NOTE];

spinner.succeed('Fetched argument values')
return commandOptions;
}
4 changes: 3 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const DEFAULT_WIDTH = '1680';
export const DEFAULT_MIN_HEIGHT = '600';
export const DEFAULT_FILENAME = 'opensearch-report';
export const DEFAULT_EMAIL_SUBJECT = 'This is an email containing your opensearch dashboard report';
export const DEFAULT_EMAIL_NOTE = 'Hi,<br>Here is the latest report!';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will users know to use <br> instead of new line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure. should we add template is html somewhere in the docs?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if new line is provided? if doesn't work as expected maybe programmatically convert text to html?

just a thought but does this need to support reading from files for easier multi-line editing? e.g. --email-body-file ./email-body.txt. or users are likely to use one-line notes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently, if new line is provided it just gets ignored
e.g. 'Hi,\nHere is the latest report!' will look like
image

We can surely add text to html logic and/or read from file. Not sure what is preferred though. will check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added logic to convert text to html and note will accept string or path to a text file which can contain verbose message.


export const REPORT_TYPE = {
DASHBOARD: 'Dashboard',
Expand Down Expand Up @@ -60,7 +61,8 @@ export const ENV_VAR = {
SMTP_SECURE: 'OPENSEARCH_SMTP_SECURE',
SMTP_USERNAME: 'OPENSEARCH_SMTP_USERNAME',
SMTP_PASSWORD: 'OPENSEARCH_SMTP_PASSWORD',
EMAIL_SUBJECT: 'OPENSEARCH_EMAIL_SUBJECT'
EMAIL_SUBJECT: 'OPENSEARCH_EMAIL_SUBJECT',
EMAIL_NOTE: 'OPENSEARCH_EMAIL_NOTE',
}

export const TRANSPORT_TYPE = {
Expand Down
15 changes: 11 additions & 4 deletions src/download-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import ora from 'ora';

const spinner = ora();

export async function downloadReport(url, format, width, height, filename, authType, username, password, tenant, time) {
export async function downloadReport(url, format, width, height, filename, authType, username, password, tenant, time, transport) {
spinner.start('Connecting to url ' + url);
try {
const browser = await puppeteer.launch({
Expand Down Expand Up @@ -132,12 +132,19 @@ export async function downloadReport(url, format, width, height, filename, authT
}
}

await browser.close();

const timeCreated = time.valueOf();
const data = { timeCreated, dataUrl: buffer.toString('base64'), };

await readStreamToFile(data.dataUrl, filename, format);

if (transport !== undefined) {
let emailTemplateImageBuffer = await page.screenshot({
rupal-bq marked this conversation as resolved.
Show resolved Hide resolved
fullPage: true,
});
const data = { timeCreated, dataUrl: emailTemplateImageBuffer.toString('base64'), };
await readStreamToFile(data.dataUrl, 'email_body.png', FORMAT.PNG);
joshuali925 marked this conversation as resolved.
Show resolved Hide resolved
}

await browser.close();
spinner.succeed('The report is downloaded');
} catch (e) {
spinner.fail('Downloading report failed. ' + e);
Expand Down
66 changes: 35 additions & 31 deletions src/email-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import nodemailer from "nodemailer";
import hbs from "nodemailer-express-handlebars";
import ora from 'ora';
import { FORMAT } from './constants.js';
import fs from 'fs';
import AWS from "aws-sdk";
import path from 'path';
import { fileURLToPath } from 'url';
Expand All @@ -22,11 +22,12 @@ try {
// Do not set AWS_SDK_LOAD_CONFIG if aws config file is missing.
}

export async function sendEmail(filename, format, sender, recipient, transport, smtphost, smtpport, smtpsecure, smtpusername, smtppassword, subject) {
export async function sendEmail(filename, url, sender, recipient, transport, smtphost, smtpport, smtpsecure, smtpusername, smtppassword, subject, note) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: I feel the param list is a little bit long, we may consider to refactor it a little bit later.

Copy link
Contributor Author

@rupal-bq rupal-bq Jan 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

created issue #12 to track this. will address this in separate PR.

if (transport !== undefined && (transport === 'smtp' || ses !== undefined) && sender !== undefined && recipient !== undefined) {
spinner.start('Sending email...');
} else {
if (transport === undefined && sender === undefined && recipient === undefined) {
deleteTemporaryImage();
return;
} else if (transport === undefined) {
spinner.warn('Transport value is missing');
Expand All @@ -36,10 +37,11 @@ export async function sendEmail(filename, format, sender, recipient, transport,
spinner.warn('Sender/Recipient value is missing');
}
spinner.fail('Skipped sending email');
deleteTemporaryImage();
return;
}

let mailOptions = getmailOptions(format, sender, recipient, filename, subject);
let mailOptions = getmailOptions(url, sender, recipient, filename, subject, note);

let transporter = getTransporter(transport, smtphost, smtpport, smtpsecure, smtpusername, smtppassword);

Expand All @@ -59,6 +61,7 @@ export async function sendEmail(filename, format, sender, recipient, transport,
} else {
spinner.succeed('Email sent successfully');
}
deleteTemporaryImage();
});
}

Expand All @@ -81,33 +84,34 @@ const getTransporter = (transport, smtphost, smtpport, smtpsecure, smtpusername,
return transporter;
}

const getmailOptions = (format, sender, recipient, file, emailSubject, mailOptions = {}) => {
if (format === FORMAT.PNG) {
mailOptions = {
from: sender,
subject: emailSubject,
to: recipient,
attachments: [
{
filename: file,
path: file,
cid: 'report'
}],
template: 'index'
};
} else {
mailOptions = {
from: sender,
subject: emailSubject,
to: recipient,
attachments: [
{
filename: file,
path: file,
contentType: 'application/pdf'
}],
template: 'index'
};
}
const getmailOptions = (url, sender, recipient, file, emailSubject, note, mailOptions = {}) => {
mailOptions = {
from: sender,
subject: emailSubject,
to: recipient,
attachments: [
{
filename: 'email_body.png',
path: 'email_body.png',
cid: 'email_body'
},
{
filename: file,
path: file
}],
template: 'index',
context: {
REPORT_TITLE: file,
DASHBOARD_URL: url,
NOTE: note
}
};
return mailOptions;
}

function deleteTemporaryImage() {
// Delete temporary image created for email body
if (fs.existsSync('email_body.png')) {
fs.unlinkSync('email_body.png');
}
}
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ await downloadReport(
options.username,
options.password,
options.tenant,
options.time
options.time,
options.transport
);

await sendEmail(
options.filename,
options.format,
options.url,
options.sender,
options.recipient,
options.transport,
Expand All @@ -37,4 +38,5 @@ await sendEmail(
options.smtpusername,
options.smtppassword,
options.subject,
options.note
);
Loading