import nodemailer from 'nodemailer'; const SMTP_HOST = process.env.SMTP_HOST || 'smtp.gmail.com'; const SMTP_PORT = parseInt(process.env.SMTP_PORT || '587'); const SMTP_SECURE = process.env.SMTP_SECURE === 'true'; const SMTP_USER = process.env.SMTP_USER; const SMTP_PASSWORD = process.env.SMTP_PASSWORD; const EMAIL_FROM = process.env.EMAIL_FROM || 'Basil Recipe Manager '; const APP_URL = process.env.APP_URL || 'http://localhost:5173'; // Create transporter const transporter = nodemailer.createTransport({ host: SMTP_HOST, port: SMTP_PORT, secure: SMTP_SECURE, auth: SMTP_USER && SMTP_PASSWORD ? { user: SMTP_USER, pass: SMTP_PASSWORD, } : undefined, }); /** * Send email verification email */ export async function sendVerificationEmail(email: string, token: string, name?: string): Promise { const verificationUrl = `${APP_URL}/verify-email/${token}`; const htmlContent = `

🌿 Basil Recipe Manager

Welcome${name ? `, ${name}` : ''}!

Thank you for registering with Basil Recipe Manager. To complete your registration and verify your email address, please click the button below:

Verify Email Address

Or copy and paste this link into your browser:

${verificationUrl}

This link will expire in 24 hours.

If you didn't create an account with Basil, you can safely ignore this email.

`; const textContent = ` Welcome to Basil Recipe Manager! ${name ? `Hi ${name},` : 'Hello,'} Thank you for registering. Please verify your email address by clicking the link below: ${verificationUrl} This link will expire in 24 hours. If you didn't create an account with Basil, you can safely ignore this email. --- Basil Recipe Manager `; try { await transporter.sendMail({ from: EMAIL_FROM, to: email, subject: 'Verify Your Email - Basil Recipe Manager', text: textContent, html: htmlContent, }); } catch (error) { console.error('Failed to send verification email:', error); throw new Error('Failed to send verification email'); } } /** * Send password reset email */ export async function sendPasswordResetEmail(email: string, token: string, name?: string): Promise { const resetUrl = `${APP_URL}/reset-password/${token}`; const htmlContent = `

🌿 Basil Recipe Manager

Password Reset Request

${name ? `Hi ${name},` : 'Hello,'}

We received a request to reset your password. Click the button below to create a new password:

Or copy and paste this link into your browser:

${resetUrl}

This link will expire in 1 hour.

If you didn't request a password reset, you can safely ignore this email. Your password will remain unchanged.

`; const textContent = ` Password Reset Request ${name ? `Hi ${name},` : 'Hello,'} We received a request to reset your password. Click the link below to create a new password: ${resetUrl} This link will expire in 1 hour. If you didn't request a password reset, you can safely ignore this email. --- Basil Recipe Manager `; try { await transporter.sendMail({ from: EMAIL_FROM, to: email, subject: 'Password Reset - Basil Recipe Manager', text: textContent, html: htmlContent, }); } catch (error) { console.error('Failed to send password reset email:', error); throw new Error('Failed to send password reset email'); } } /** * Test email configuration */ export async function testEmailConfig(): Promise { try { await transporter.verify(); console.log('✓ Email configuration is valid'); return true; } catch (error) { console.error('✗ Email configuration error:', error); return false; } }