-
Notifications
You must be signed in to change notification settings - Fork 13
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
Changes from 3 commits
0086bcf
14a0dbb
cfc7aff
991ae6f
6594bb5
0695340
546cffd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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!'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will users know to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure. should we add template is html somewhere in the docs? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', | ||
|
@@ -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 = { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
|
@@ -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); | ||
|
||
|
@@ -59,6 +61,7 @@ export async function sendEmail(filename, format, sender, recipient, transport, | |
} else { | ||
spinner.succeed('Email sent successfully'); | ||
} | ||
deleteTemporaryImage(); | ||
}); | ||
} | ||
|
||
|
@@ -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'); | ||
} | ||
} |
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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](https://user-images.githubusercontent.com/56703525/214383303-406be59f-914f-4afd-af0f-52a2455d0d59.png)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?