Files
Checkmate/Server/utils/sendEmail.js
T

36 lines
1.3 KiB
JavaScript
Raw Normal View History

2024-06-05 14:17:54 -04:00
const sgMail = require('@sendgrid/mail')
const logger = require('./logger')
sgMail.setApiKey(process.env.SENDGRID_API_KEY)
const SERVICE_NAME = 'Email_Service';
2024-05-13 19:20:56 -04:00
/**
2024-06-05 14:17:54 -04:00
* @async
2024-05-13 19:20:56 -04:00
* @function
2024-06-05 14:17:54 -04:00
* @param {[String]} receivers - takes an array of strings
* @param {String} subject - takes a single string
* @param {String} contentHTML - takes a single string that contains HTML
* @param {String} contentText - takes a string to be used if contentHTML is not compatible
* @example
* await sendEmail(['veysel@bluewavelabs.ca','alex@bluewavelabs.ca'],'Testing Email Service','<h1>BlueWaveLabs</h1>','Testing Email Service')
2024-05-13 19:20:56 -04:00
*/
2024-06-05 14:17:54 -04:00
const sendEmail = async (receivers,subject, contentHTML, contentText = null ) => {
const msg = {
to: receivers,
from: {
name: 'Uptime System',
email: process.env.SYSTEM_EMAIL_ADDRESS // must be verified email by sendgrid
},
subject: subject,
text: contentText || contentHTML,
html: contentHTML,
}
2024-05-13 19:20:56 -04:00
try {
2024-06-05 14:17:54 -04:00
await sgMail.send(msg);
logger.info(`Emails sent to receivers:${receivers} with the subject:${subject}`,{service:SERVICE_NAME})
2024-05-13 19:20:56 -04:00
} catch (error) {
2024-06-05 14:17:54 -04:00
logger.error(`Sending Email action failed, ERROR:${error}`, { service: SERVICE_NAME });
2024-05-13 19:20:56 -04:00
}
}
2024-06-05 14:17:54 -04:00
2024-05-13 19:20:56 -04:00
module.exports = {sendEmail}