const FormData = require('form-data'); const Mailgun = require('mailgun.js'); const mailgun = new Mailgun(FormData); const mg = process.env.MAILGUN_API_KEY ? mailgun.client({ username: 'api', key: process.env.MAILGUN_API_KEY, // EU customers: set MAILGUN_API_URL=https://api.eu.mailgun.net url: process.env.MAILGUN_API_URL || 'https://api.mailgun.net', }) : null; const DOMAIN = process.env.MAILGUN_DOMAIN; const FROM = process.env.EMAIL_FROM || (DOMAIN ? `BlindMaster ` : null); async function sendMail({ to, subject, html }) { if (!mg || !DOMAIN) { console.warn('[mailer] Mailgun not configured (MAILGUN_API_KEY / MAILGUN_DOMAIN missing). Skipping send:', subject); return null; } return mg.messages.create(DOMAIN, { from: `"BlindMaster" <${FROM}>`, to: [to], subject, html, }); } // Helper function to get color based on time of day // hour parameter should be the local hour (0-23) from the client function getColorForTime(hour) { if (hour >= 5 && hour < 10) { // Morning - orange return '#FF9800'; } else if (hour >= 10 && hour < 18) { // Afternoon - blue return '#2196F3'; } else { // Evening/Night - purple return '#471189'; } } // Helper function to send email async function sendVerificationEmail(toEmail, token, name, localHour = new Date().getHours()) { const primaryColor = getColorForTime(localHour); const verificationLink = `https://blindmaster.wahwa.com/verify-email?token=${token}`; try { const info = await sendMail({ to: toEmail, subject: "Verify your BlindMaster account", html: `

BlindMaster

Smart Home Automation

Welcome${name && name.trim() ? `, ${name.trim()}` : ''}!

Thank you for joining BlindMaster! To electrify your blinds, please verify your email address 🥹

Verify Email Address

This verification link will expire in 15 minutes.

If you didn't create a BlindMaster account, please ignore this email!!!

© 2026 BlindMaster. All rights reserved.

`, }); console.log("Verification email sent:", info?.id || '(skipped)'); return true; } catch (error) { console.error("Error sending verification email:", error); return false; } } // Helper function to send password reset email async function sendPasswordResetEmail(toEmail, code, name, localHour = new Date().getHours()) { const primaryColor = getColorForTime(localHour); try { const info = await sendMail({ to: toEmail, subject: "Reset your BlindMaster password", html: `

BlindMaster

Smart Home Automation

Password Reset Request

${name && name.trim() ? `Hi ${name.trim()}, we` : 'We'} received a request to reset your password. Use the code below to continue:

${code}

This code will expire in 15 minutes.

If you didn't request a password reset, please ignore this email.

© 2026 BlindMaster. All rights reserved.

`, }); console.log("Password reset email sent:", info?.id || '(skipped)'); return true; } catch (error) { console.error("Error sending password reset email:", error); return false; } } // Helper function to send email change verification email async function sendEmailChangeVerification(newEmail, token, name, oldEmail, localHour = new Date().getHours()) { const primaryColor = getColorForTime(localHour); const verificationLink = `https://blindmaster.wahwa.com/verify-email-change?token=${token}`; try { const info = await sendMail({ to: newEmail, subject: "Verify your new BlindMaster email address", html: `

BlindMaster

Smart Home Automation

Verify Your New Email

${name && name.trim() ? `Hi ${name.trim()}, you` : 'You'} requested to change your email address from:

${oldEmail}

Click the button below to confirm this change:

Verify New Email

This verification link will expire in 15 minutes.

If you didn't request this email change, please ignore this email.

© 2026 BlindMaster. All rights reserved.

`, }); console.log("Email change verification sent:", info?.id || '(skipped)'); return true; } catch (error) { console.error("Error sending email change verification:", error); return false; } } // Helper function to generate styled HTML response for verification pages function generateVerificationPageHTML(title, message, isSuccess = true) { const primaryColor = '#2196F3'; // Blue theme const icon = isSuccess ? '✓' : '✕'; const iconBg = isSuccess ? primaryColor : '#f44336'; return ` ${title} - BlindMaster

BlindMaster

Smart Home Automation

${icon}

${title}

${message}

You can safely close this window and return to the app.

© 2026 BlindMaster. All rights reserved.

`; } module.exports = { sendVerificationEmail, sendPasswordResetEmail, sendEmailChangeVerification, generateVerificationPageHTML, };