From 4ec8369fc7349e9430deb2670d0846b54efecccd Mon Sep 17 00:00:00 2001 From: subinqkim Date: Sat, 2 Dec 2023 16:09:44 -0800 Subject: [PATCH] Added nodeMailer.js code and transporter.js that is called within nodeMailer --- routes/nodeMailer.js | 28 +++++++++++++++++++++++++++- transporter.js | 18 ++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 transporter.js diff --git a/routes/nodeMailer.js b/routes/nodeMailer.js index 4a10ec6..2a09cae 100644 --- a/routes/nodeMailer.js +++ b/routes/nodeMailer.js @@ -1 +1,27 @@ -//setup \ No newline at end of file +const express = require('express'); +const transporter = require('../transporter'); +// TODO: add verifyToken + +const emailRouter = express(); + +emailRouter.use(express.json()); + +emailRouter.post('/send', (req, res) => { + const { email, messageHtml, subject } = req.body; + const mail = { + from: `${process.env.REACT_APP_EMAIL_FIRST_NAME} ${process.env.REACT_APP_EMAIL_LAST_NAME} ${process.env.REACT_APP_EMAIL_USERNAME}`, + to: email, + subject, + html: messageHtml, + }; + + transporter.sendMail(mail, (err) => { + if (err) { + res.status(500).send(`Transporter Error: ${err}`); + } else { + res.status(200).send('Transporter Backend Successfully Sent'); + } + }); +}); + +module.exports = emailRouter; \ No newline at end of file diff --git a/transporter.js b/transporter.js new file mode 100644 index 0000000..fdff8c1 --- /dev/null +++ b/transporter.js @@ -0,0 +1,18 @@ +const nodemailer = require('nodemailer'); + +require('dotenv').config(); + +// sender information +const transport = { + host: 'smtp.gmail.com', // e.g. smtp.gmail.com + auth: { + user: process.env.REACT_APP_EMAIL_USERNAME, + pass: process.env.REACT_APP_EMAIL_PASSWORD, + }, + from: process.env.REACT_APP_EMAIL_USERNAME, + secure: true, +}; + +const transporter = nodemailer.createTransport(transport); + +module.exports = transporter; \ No newline at end of file