diff --git a/functions/index.js b/functions/index.js index 4a90329..efcca6a 100644 --- a/functions/index.js +++ b/functions/index.js @@ -5,6 +5,7 @@ const getTodo = require("./getTodo"); const getExplara = require("./explaraProxy"); const getCount = require("./getCount"); const generateCertificate = require("./generateCertificate"); +const mailer = require("./mailer"); module.exports = { echo, @@ -13,5 +14,6 @@ module.exports = { getTime, getExplara, getCount, - generateCertificate + generateCertificate, + mailer }; diff --git a/functions/mailer.js b/functions/mailer.js new file mode 100644 index 0000000..3cbf1e3 --- /dev/null +++ b/functions/mailer.js @@ -0,0 +1,30 @@ +const nodemailer = require("nodemailer"); +const transporter = nodemailer.createTransport({ + service: "gmail", + host: "smtp.gmail.com", + port: 587, + pool: true, + auth: { + user: "jaisondennis@mec.ac.in", + pass: "*#Jaison77#*", + }, +}); + +const main = async (body) => { + try { + const mailOptions = { + from: "iedcmec@mec.ac.in", + to: body.toEmail, + subject: body.subject, + html: body.content, + }; + await transporter.sendMail(mailOptions); + console.log(`Sent mail to ${body.toEmail} with subject ${body.subject}`); + } catch (error) { + console.error( + `Failed to send email to ${body.toEmail} having error : ${error}` + ); + } +}; + +module.exports = main; diff --git a/index.js b/index.js index 664a488..5fa96df 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ const express = require("express"); const fs = require("fs"); +const cors = require("cors"); const { echo, getTime, @@ -8,10 +9,15 @@ const { getExplara, getCount, generateCertificate, + mailer } = require("./functions"); const app = express(); +app.use(express.urlencoded({ extended: true })); +app.use(express.json()); +app.use(cors()); + app.get("/time", (_, res) => { const t = getTime(); res.send(t); @@ -57,6 +63,11 @@ app.get("/certificate", async (_, res) => { res.send(data); }); +app.post("/mailer", async (_, res) => { + const data = await mailer(_.body); + res.send(data); +}); + app.get("/registrations", async (_, res) => { const filePath = __dirname + "/html/totalRegistration.html"; res.sendFile(filePath); diff --git a/package-lock.json b/package-lock.json index 95435bf..31657d1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "@sparticuz/chromium": "^111.0.0", "axios": "^1.3.4", + "cors": "^2.8.5", "csv-parser": "^3.0.0", "dotenv": "^16.0.3", "express": "^4.18.2", @@ -805,6 +806,18 @@ "version": "1.0.6", "license": "MIT" }, + "node_modules/cors": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", + "dependencies": { + "object-assign": "^4", + "vary": "^1" + }, + "engines": { + "node": ">= 0.10" + } + }, "node_modules/cosmiconfig": { "version": "8.0.0", "license": "MIT", @@ -1708,6 +1721,14 @@ "node": ">=0.10.0" } }, + "node_modules/object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/object-inspect": { "version": "1.12.3", "license": "MIT", @@ -3137,6 +3158,15 @@ "cookie-signature": { "version": "1.0.6" }, + "cors": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", + "requires": { + "object-assign": "^4", + "vary": "^1" + } + }, "cosmiconfig": { "version": "8.0.0", "requires": { @@ -3695,6 +3725,11 @@ "version": "3.0.0", "dev": true }, + "object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==" + }, "object-inspect": { "version": "1.12.3" }, diff --git a/package.json b/package.json index 3ad6792..222e15a 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,8 @@ "description": "", "main": "index.js", "scripts": { - "start": "isDev=true node index.js", - "dev": "isDev=true nodemon index.js" + "start": "node index.js", + "dev": "nodemon index.js" }, "keywords": [ "lambda functions", @@ -17,6 +17,7 @@ "dependencies": { "@sparticuz/chromium": "^111.0.0", "axios": "^1.3.4", + "cors": "^2.8.5", "csv-parser": "^3.0.0", "dotenv": "^16.0.3", "express": "^4.18.2",