72 lines
2.3 KiB
JavaScript
72 lines
2.3 KiB
JavaScript
import { title } from "node:process"
|
|
import { globalConfig } from "../index.js"
|
|
import * as nodemailer from 'nodemailer'
|
|
|
|
const transporter = nodemailer.createTransport({
|
|
host: globalConfig.email.host,
|
|
port: 465,
|
|
secure: true,
|
|
auth: {
|
|
user: globalConfig.email.username,
|
|
pass: globalConfig.email.password
|
|
}
|
|
})
|
|
|
|
/**
|
|
* Global send email function, takes recipient, subject line and content line
|
|
* @param {string} recipient recipients email address
|
|
* @param {string} subject email subject line
|
|
* @param {string} content email content
|
|
* @returns {object} {success: bool, message: string}
|
|
*/
|
|
function sendEmail(recipient, subject, content) {
|
|
return transporter.sendMail({
|
|
from: "Enstrayed API <api@enstrayed.com>",
|
|
to: rreq.body.recipient,
|
|
subject: rreq.body.subject ?? "Subject Not Set",
|
|
text: rreq.body.message ?? "Message Not Set"
|
|
}).then(transportResponse => {
|
|
if (transportResponse.response.slice(0, 1) === "2") {
|
|
return { success: true, message: transportResponse.response }
|
|
} else {
|
|
return { success: false, message: transportResponse.response }
|
|
}
|
|
}).catch(transportError => {
|
|
return { success: false, message: transportError }
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Send a discord webhoook to supplied URL with an embed
|
|
* @param {string} webhook Webhook URL
|
|
* @param {string} subject Message subject line
|
|
* @param {string} content Message content
|
|
* @param {string} sender Sender shown on footer of embed
|
|
* @param {string} color hex color of embed accent
|
|
* @returns {object} {success: bool, message: string}
|
|
*/
|
|
function sendWebhookDiscord(webhook, subject, content, sender, color) {
|
|
let embed = {
|
|
title: subject,
|
|
description: content,
|
|
color: parseInt(color, 16) ?? 16777215,
|
|
footer: {
|
|
text: `Enstrayed API - Sent by ${sender ?? "SENDER"}`
|
|
}
|
|
}
|
|
return fetch(webhook, {
|
|
method: "POST",
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({ embeds: [embed] })
|
|
}).then(response => {
|
|
if (response.status === 204) {
|
|
return { success: true, message: response.status}
|
|
} else {
|
|
return { success: false, message: response.status}
|
|
}
|
|
})
|
|
}
|
|
|
|
export { sendEmail, sendWebhookDiscord } |