Files
blinds_express/mailer.js

241 lines
10 KiB
JavaScript
Raw Normal View History

2026-01-07 17:29:00 -06:00
const nodemailer = require('nodemailer');
2026-01-08 09:52:20 -06:00
const { SESv2Client, SendEmailCommand } = require('@aws-sdk/client-sesv2');
2026-01-07 17:29:00 -06:00
2026-01-08 09:55:28 -06:00
const sesClient = new SESv2Client({
2026-01-07 17:29:00 -06:00
region: process.env.AWS_REGION,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
},
});
// Create the transporter
const transporter = nodemailer.createTransport({
2026-01-08 09:55:28 -06:00
SES: { sesClient, SendEmailCommand },
2026-01-07 17:29:00 -06:00
});
2026-01-08 13:22:32 -06:00
// Helper function to get color based on time of day
function getColorForTime() {
const hour = new Date().getHours();
if (hour >= 5 && hour < 10) {
// Morning - orange
return {
primary: '#FF9800',
gradient: 'linear-gradient(135deg, #FF9800 0%, #F57C00 100%)',
shadow: 'rgba(255, 152, 0, 0.3)'
};
} else if (hour >= 10 && hour < 18) {
// Afternoon - blue
return {
primary: '#2196F3',
gradient: 'linear-gradient(135deg, #2196F3 0%, #005CA8 100%)',
shadow: 'rgba(33, 150, 243, 0.3)'
};
} else {
// Evening/Night - purple
return {
primary: '#471189',
gradient: 'linear-gradient(135deg, #BA82FF 0%, #280059 100%)',
shadow: 'rgba(71, 17, 137, 0.3)'
};
}
}
2026-01-07 17:29:00 -06:00
// Helper function to send email
async function sendVerificationEmail(toEmail, token, name) {
2026-01-08 13:22:32 -06:00
const colors = getColorForTime();
2026-01-07 17:29:00 -06:00
const verificationLink = `https://wahwa.com/verify-email?token=${token}`;
try {
const info = await transporter.sendMail({
from: `"BlindMaster" <${process.env.EMAIL_FROM}>`, // Sender address
to: toEmail,
subject: "Verify your BlindMaster account",
html: `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css2?family=ABeeZee:ital@0;1&display=swap" rel="stylesheet">
</head>
<body style="margin: 0; padding: 0; background-color: #f5f5f5; font-family: 'ABeeZee', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;">
<table width="100%" cellpadding="0" cellspacing="0" style="background-color: #f5f5f5; padding: 40px 0;">
<tr>
<td align="center">
<table width="600" cellpadding="0" cellspacing="0" style="background-color: #ffffff; border-radius: 12px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); overflow: hidden; max-width: 600px;">
<!-- Header with brand color -->
<tr>
2026-01-08 13:22:32 -06:00
<td align="center" style="background: ${colors.gradient}; padding: 40px 20px;">
<h1 style="margin: 0; color: #ffffff; font-size: 32px; font-weight: bold; letter-spacing: 0.5px;">BlindMaster</h1>
<p style="margin: 10px 0 0 0; color: #ffffff; font-size: 14px; opacity: 0.95;">Smart Home Automation</p>
</td>
</tr>
<!-- Welcome message -->
<tr>
<td style="padding: 50px 40px 30px 40px; text-align: center;">
<h2 style="margin: 0 0 20px 0; color: #333333; font-size: 28px; font-weight: normal;">
2026-01-08 13:22:32 -06:00
Welcome${name && name.trim() ? `, <span style="color: ${colors.primary};">${name.trim()}</span>` : ''}!
</h2>
<p style="margin: 0 0 30px 0; color: #666666; font-size: 16px; line-height: 1.6;">
Thank you for joining BlindMaster! To electrify your blinds, please verify your email address 🥹
</p>
</td>
</tr>
<!-- CTA Button -->
<tr>
<td align="center" style="padding: 0 40px 40px 40px;">
<a href="${verificationLink}"
2026-01-08 13:22:32 -06:00
style="display: inline-block; padding: 16px 48px; background: ${colors.gradient}; color: #ffffff; text-decoration: none; border-radius: 8px; font-size: 16px; font-weight: bold; box-shadow: 0 4px 12px ${colors.shadow}; transition: all 0.3s ease;">
Verify Email Address
</a>
</td>
</tr>
<!-- Divider -->
<tr>
<td style="padding: 0 40px;">
<div style="border-top: 1px solid #e0e0e0;"></div>
</td>
</tr>
<!-- Footer info -->
<tr>
<td style="padding: 30px 40px; text-align: center;">
<p style="margin: 0 0 10px 0; color: #999999; font-size: 13px; line-height: 1.5;">
This verification link will expire in <strong style="color: #666666;">24 hours</strong>.
</p>
<p style="margin: 0; color: #999999; font-size: 13px; line-height: 1.5;">
If you didn't create a BlindMaster account, please ignore this email!!!
</p>
</td>
</tr>
<!-- Footer bar -->
<tr>
<td align="center" style="background-color: #f9f9f9; padding: 25px 40px;">
<p style="margin: 0; color: #999999; font-size: 12px;">
© 2026 BlindMaster. All rights reserved.
</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
2026-01-07 17:29:00 -06:00
`,
});
console.log("Email sent successfully:", info.messageId);
return true;
} catch (error) {
console.error("Error sending email:", error);
return false;
}
}
2026-01-08 13:06:20 -06:00
// Helper function to send password reset email
async function sendPasswordResetEmail(toEmail, code, name) {
2026-01-08 13:22:32 -06:00
const colors = getColorForTime();
2026-01-08 13:06:20 -06:00
try {
const info = await transporter.sendMail({
from: `"BlindMaster" <${process.env.EMAIL_FROM}>`,
to: toEmail,
subject: "Reset your BlindMaster password",
html: `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css2?family=ABeeZee:ital@0;1&display=swap" rel="stylesheet">
</head>
<body style="margin: 0; padding: 0; background-color: #f5f5f5; font-family: 'ABeeZee', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;">
<table width="100%" cellpadding="0" cellspacing="0" style="background-color: #f5f5f5; padding: 40px 0;">
<tr>
<td align="center">
<table width="600" cellpadding="0" cellspacing="0" style="background-color: #ffffff; border-radius: 12px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); overflow: hidden; max-width: 600px;">
<!-- Header with brand color -->
<tr>
2026-01-08 13:22:32 -06:00
<td align="center" style="background: ${colors.gradient}; padding: 40px 20px;">
2026-01-08 13:06:20 -06:00
<h1 style="margin: 0; color: #ffffff; font-size: 32px; font-weight: bold; letter-spacing: 0.5px;">BlindMaster</h1>
<p style="margin: 10px 0 0 0; color: #ffffff; font-size: 14px; opacity: 0.95;">Smart Home Automation</p>
</td>
</tr>
<!-- Message -->
<tr>
<td style="padding: 50px 40px 30px 40px; text-align: center;">
<h2 style="margin: 0 0 20px 0; color: #333333; font-size: 28px; font-weight: normal;">
Password Reset Request
</h2>
<p style="margin: 0 0 30px 0; color: #666666; font-size: 16px; line-height: 1.6;">
${name && name.trim() ? `Hi ${name.trim()}, we` : 'We'} received a request to reset your password. Use the code below to continue:
</p>
</td>
</tr>
<!-- Code Display -->
<tr>
<td align="center" style="padding: 0 40px 40px 40px;">
2026-01-08 13:22:32 -06:00
<div style="display: inline-block; padding: 20px 40px; background-color: #f9f9f9; border-radius: 8px; border: 2px solid ${colors.primary};">
<p style="margin: 0; color: ${colors.primary}; font-size: 36px; font-weight: bold; letter-spacing: 8px; font-family: 'Courier New', monospace;">
2026-01-08 13:06:20 -06:00
${code}
</p>
</div>
</td>
</tr>
<!-- Divider -->
<tr>
<td style="padding: 0 40px;">
<div style="border-top: 1px solid #e0e0e0;"></div>
</td>
</tr>
<!-- Footer info -->
<tr>
<td style="padding: 30px 40px; text-align: center;">
<p style="margin: 0 0 10px 0; color: #999999; font-size: 13px; line-height: 1.5;">
This code will expire in <strong style="color: #666666;">15 minutes</strong>.
</p>
<p style="margin: 0; color: #999999; font-size: 13px; line-height: 1.5;">
If you didn't request a password reset, please ignore this email.
</p>
</td>
</tr>
<!-- Footer bar -->
<tr>
<td align="center" style="background-color: #f9f9f9; padding: 25px 40px;">
<p style="margin: 0; color: #999999; font-size: 12px;">
© 2026 BlindMaster. All rights reserved.
</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
`,
});
console.log("Password reset email sent successfully:", info.messageId);
return true;
} catch (error) {
console.error("Error sending password reset email:", error);
return false;
}
}
module.exports = { sendVerificationEmail, sendPasswordResetEmail };