Skip to content
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

110 add email confirmation #212

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 5 additions & 26 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 43 additions & 35 deletions backend/routes/emailRoutes.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,75 @@
const express = require("express");
const nodemailer = require("nodemailer");
const multer = require("multer");
const router = express.Router()
const router = express.Router();

require("dotenv/config")
require("dotenv/config");

// POST /api/email/
router.post('/', async (req, res) => {
router.post("/", async (req, res) => {
try {
const { recipientEmail, subject, body, isHTML } = req.body;

const transporter = nodemailer.createTransport({
service: 'Gmail',
service: "Gmail",
auth: {
user: process.env.email_address,
pass: process.env.email_password,
user: process.env.H4H_EMAIL,
pass: process.env.H4H_EMAIL_PASSWORD,
},
});

let mailOptions = ''
let mailOptions = "";
// Prepare the email message
if (isHTML) {
mailOptions = {
from: process.env.email_address,
from: process.env.H4H_EMAIL,
to: recipientEmail,
subject: subject ,
subject: subject,
html: body, // Use the provided body as HTML
};
} else {
mailOptions = {
from: process.env.email_address,
from: process.env.H4H_EMAIL,
to: recipientEmail,
subject: subject ,
subject: subject,
text: body, // Use the provided body as plain text
};
}

// Send the email
await transporter.sendMail(mailOptions);
res.status(200).json({ message: 'Custom email sent successfully' }); // Return a success response

res.status(200).json({ message: "Custom email sent successfully" }); // Return a success response
} catch (error) {
console.log(error)
res.status(500).json({ error: 'An error occurred while sending the custom email' }); // Return an error response
console.log(error);
res
.status(500)
.json({ error: "An error occurred while sending the custom email" }); // Return an error response
}
});


// POST /api/email/attach-files
const storage = multer.memoryStorage(); // Create a Multer storage configuration
const upload = multer({ storage });

router.post('/attach-files', upload.array('attachments'), async (req, res) => {
router.post("/attach-files", upload.array("attachments"), async (req, res) => {
try {
const { recipientEmail, subject, body, isHTML } = req.body;
const attachments = req.files;

const transporter = nodemailer.createTransport({
service: 'Gmail',
service: "Gmail",
auth: {
user: process.env.email_address,
pass: process.env.email_password,
user: process.env.H4H_EMAIL,
pass: process.env.H4H_EMAIL_PASSWORD,
},
});

let mailOptions = '';
let mailOptions = "";
// Prepare the email message
if (isHTML) {
mailOptions = {
from: process.env.email_address,
from: process.env.H4H_EMAIL,
to: recipientEmail,
subject: subject,
html: body, // Use the provided body as HTML
Expand All @@ -80,7 +81,7 @@ router.post('/attach-files', upload.array('attachments'), async (req, res) => {
};
} else {
mailOptions = {
from: process.env.email_address,
from: process.env.H4H_EMAIL,
to: recipientEmail,
subject: subject,
text: body, // Use the provided body as plain text
Expand All @@ -95,32 +96,34 @@ router.post('/attach-files', upload.array('attachments'), async (req, res) => {
// Send the email
await transporter.sendMail(mailOptions);

res.status(200).json({ message: 'Custom email sent successfully' }); // Return a success response
res.status(200).json({ message: "Custom email sent successfully" }); // Return a success response
} catch (error) {
console.log(error);
res.status(500).json({ error: 'An error occurred while sending the custom email' }); // Return an error response
res
.status(500)
.json({ error: "An error occurred while sending the custom email" }); // Return an error response
}
});

// POST /api/email/donation-approved-scheduled
router.post('/donation-approved-scheduled', async (req, res) => {
router.post("/donation-approved-scheduled", async (req, res) => {
try {
const { recipientEmail, donationDetails } = req.body;

// Create a Nodemailer transporter
const transporter = nodemailer.createTransport({
service: 'Gmail',
service: "Gmail",
auth: {
user: process.env.email_address,
pass: process.env.email_password,
user: process.env.H4H_EMAIL,
pass: process.env.H4H_EMAIL_PASSWORD,
},
});

// Prepare the email message
const mailOptions = {
from: process.env.email_address,
from: process.env.H4H_EMAIL,
to: recipientEmail,
subject: 'Donation Approved & Scheduled',
subject: "Donation Approved & Scheduled",
html: `<p>${donationDetails.name},</p>
<p>Thank you for your donation!</p>
<p>Your donation has been approved and scheduled for pickup or drop-off. Here are the details:</p>
Expand All @@ -136,12 +139,17 @@ router.post('/donation-approved-scheduled', async (req, res) => {
await transporter.sendMail(mailOptions);

// Return a success response
res.status(200).json({ message: 'Donation approval and schedule email sent successfully' });
res.status(200).json({
message: "Donation approval and schedule email sent successfully",
});
} catch (error) {
console.error('Error sending donation approval and schedule email:', error);
console.error("Error sending donation approval and schedule email:", error);
// Return an error response
res.status(500).json({ error: 'An error occurred while sending the donation approval and schedule email' });
res.status(500).json({
error:
"An error occurred while sending the donation approval and schedule email",
});
}
});

module.exports = router
module.exports = router;
52 changes: 52 additions & 0 deletions frontend/src/api/email.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const emailURL = "http://localhost:3001/api/email/";
/* ----------------------POST Requests---------------------------*/

// Email data model
export interface Email {
recipientEmail: string;
subject: string;
body: string | HTMLElement;
isHTML: boolean;
}

// Send an email without attachment
export const sendEmail = async (email: Email) =>
fetch(emailURL, {
headers: {
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify({
recipientEmail: email.recipientEmail,
subject: email.subject,
body: email.body,
isHTML: email.isHTML,
}),
})
.then(async (res) => {
const response = await res.json();
if (!res.ok) {
// check server response
throw new Error(`${res.status}-${res.statusText}`);
}
return response;
})
.catch((error) => console.error("Error: ", error)); // handle error

/* --------------Functions for Creating HTMLElements-------------- */
export const createParagraph = (text: string) => {
const paragraph = document.createElement("p");
paragraph.style.fontFamily = "Rubik, sans-serif";
paragraph.innerText = text;
return paragraph;
};

export const createListItem = (label: string, value: any) => {
const listItem = document.createElement("li");
const strong = document.createElement("strong");
strong.style.fontFamily = "Rubik, sans-serif";
strong.innerText = label;
listItem.appendChild(strong);
listItem.appendChild(document.createTextNode(String(value)));
return listItem;
};
Loading