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

#4 Added Email Functionality #23

Open
wants to merge 1 commit into
base: main
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
7,267 changes: 7,267 additions & 0 deletions blockchain/package-lock.json

Large diffs are not rendered by default.

1,575 changes: 798 additions & 777 deletions blockchain/yarn.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions frontend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ NEXT_PUBLIC_RAPIDAPI_KEY1=your_rapidapi_key1
NEXT_PUBLIC_RAPIDAPI_KEY2=your_rapidapi_key2 # optional
NEXT_PUBLIC_RAPIDAPI_KEY3=your_rapidapi_key3 # optional
MISTRAL_API_KEY=your_mistral_api_key
NEXT_PUBLIC_EMAIL_USERNAME=email_username
NEXT_PUBLIC_EMAIL_PASSWORD=email_password(app_password)

#The APIs used have very low limits thats why we used 3 different keys(3 accounts)... You can create them accordingly.
159 changes: 159 additions & 0 deletions frontend/app/api/email/route.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
"use server";
import nodemailer from "nodemailer";

export async function POST(req) {
const {
emailUser,
emailPass,
recipientEmails,
senderName,
subject,
placeholders,
templateId,
} = await req.json();

console.log(
emailUser,
emailPass,
recipientEmails,
senderName,
subject,
placeholders,
templateId
);

if (
!emailUser ||
!emailPass ||
!recipientEmails.length ||
!subject ||
!templateId ||
!senderName
) {
return new Response(JSON.stringify({ error: "All fields are required." }), {
status: 400,
headers: { "Content-Type": "application/json" },
});
}

const templates = [
{
id: 1,
template: "Hello [name],\n\nThis is a reminder about [event]. Please remember to [action].\n\nBest,\n[Your Name]",
placeholders: ["name", "event", "action"]
},
{
id: 2,
template: `
<div style="font-family: Arial, sans-serif; font-size: 16px; color: #333;">
<h2>Welcome to Carpool App, [name]!</h2>
<p>Thank you for registering with EtherWheels.</p>
<p>We are excited to have you on board and help you find or share rides conveniently.</p>
<p>If you have any questions or need support, feel free to reach out.</p>
<p>Best regards,</p>
<p>EtherWheels Team</p>
</div>
`,
placeholders: ["name"]
},
{
id: 3,
template: `
<div style="font-family: Arial, sans-serif; font-size: 16px; color: #333;">
<h2>Ride Creation Confirmation</h2>
<p>Dear [name],</p>
<p>Your ride has been successfully created!</p>
<p><strong>Ride Details:</strong></p>
<ul>
<li><strong>Date & Time:</strong> [rideTime]</li>
<li><strong>Pickup Location:</strong> [pickupLocation]</li>
<li><strong>Destination:</strong> [destination]</li>
</ul>
<p>Thank you for offering to provide this ride. We will notify you when passengers book this ride.</p>
<p>Safe travels!</p>
<p>Best regards,</p>
<p>EtherWheels Team</p>

</div>

`,
placeholders: ["name", "age", "rideTime", "pickupLocation", "destination"]
},
{
id: 4,
template: `
<div style="font-family: Arial, sans-serif; font-size: 16px; color: #333;">
<h2>Ride Completed Successfully</h2>
<p>Dear [name],</p>
<p>We are happy to inform you that your ride with [driverName] has been successfully completed.</p>
<p><strong>Ride Summary:</strong></p>
<ul>
<li><strong>Date & Time:</strong> [rideTime]</li>
<li><strong>Pickup Location:</strong> [pickupLocation]</li>
<li><strong>Destination:</strong> [destination]</li>
</ul>
<p>Thank you for choosing Carpool App. We hope you had a pleasant experience!</p>
<p>We look forward to serving you again in the future. If you have any feedback or questions, feel free to reach out.</p>
<p>Best regards,</p>
<p>[Your Name] from Carpool App</p>
</div>
`,
placeholders: ["name", "driverName", "rideTime", "pickupLocation", "destination"]
}
];

const selectedTemplate = templates.find((t) => t.id === parseInt(templateId));

if (!selectedTemplate) {
return new Response(JSON.stringify({ error: "Invalid template ID." }), {
status: 400,
headers: { "Content-Type": "application/json" },
});
}

let emailContent = selectedTemplate.template;
for (const [placeholder, value] of Object.entries(placeholders)) {
emailContent = emailContent.replace(`[${placeholder}]`, value);
}
emailContent = emailContent.replace("[Your Name]", senderName);

let transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
user: emailUser,
pass: emailPass,
},
});

try {
let info = await transporter.sendMail({
from: `${senderName} <${emailUser}>`,
to: recipientEmails.join(","),
subject: subject,
html: emailContent,
});

console.log("Message sent: %s", info.messageId);
return new Response(
JSON.stringify({ message: "Email sent successfully" }),
{
status: 200,
headers: { "Content-Type": "application/json" },
}
);
} catch (error) {
console.error(error);
return new Response(
JSON.stringify({
message: "Error sending email",
error: error.message,
}),
{
status: 500,
headers: { "Content-Type": "application/json" },
}
);
}
}
Loading