const nodemailer = require('nodemailer');
// Configure Gmail transporter
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.GMAIL_USER, // your Gmail
pass: process.env.GMAIL_PASS // Gmail App Password
}
});
// In-memory rate limit tracker: email → [timestamps]
const emailSendLog = new Map();
// Helper: Can we send this email?
function canSendEmail(email) {
const now = Date.now();
const windowTime = 24 * 60 * 60 * 1000; // 24 hours in ms
const limit = 2;
const timestamps = emailSendLog.get(email) || [];
const recent = timestamps.filter(ts => now - ts < windowTime);
if (recent.length >= limit) return false;
recent.push(now);
emailSendLog.set(email, recent);
return true;
}
// Send acknowledgment email to the user
async function sendAcknowledgementEmail(firstName, email) {
console.log('Sending acknowledgement email to user:', firstName, email);
const mailOptions = {
from: `"GuardianCall.ai" <${process.env.GMAIL_USER}>`,
to: email,
subject: 'Welcome to GuardianCall.ai!',
html: `
Welcome to GuardianCall.ai
|
Welcome, ${firstName}!
Thank you for signing up with GuardianCall.ai. Your account registration request has been received and is currently under review.
Once your account is approved, you'll receive a confirmation email with next steps and login instructions.
In the meantime, feel free to explore more about what GuardianCall.ai can do for you.
Warm regards, The GuardianCall.ai Team
|
|
© 2025 GuardianCall.ai. All rights reserved.
|
|
`
};
return transporter.sendMail(mailOptions);
}
// Send signup request to admin + acknowledgment to user
async function sendSignupEmail(email, firstName, lastName, password) {
if (!canSendEmail(email)) {
const error = new Error(`Rate limit exceeded for ${email}. Please try again later.`);
error.status = 429; // Too Many Requests
throw error;
}
const mailOptions = {
from: `"GuardianCall.ai" <${process.env.GMAIL_USER}>`,
to: process.env.gmail_admin,
subject: 'Signup request',
html: `
New Signup Request - GuardianCall
|
New Signup Request Received
Hi Admin,
A new user has requested signup on GuardianCall.ai.
User Details:
- First Name: ${firstName}
- Last Name: ${lastName}
- Email: ${email}
- Suggested Password: ${password}
Please verify the information and take appropriate action.
Cheers, The GuardianCall.ai Team
|
|
© 2025 GuardianCall.ai. All rights reserved.
|
|
`
};
// Send both emails
await sendAcknowledgementEmail(firstName, email);
return transporter.sendMail(mailOptions);
}
module.exports = { sendSignupEmail };