import { Resend } from 'resend'; import crypto from 'crypto'; const resend = new Resend(process.env.RESEND_API_KEY); export function generateEmailConfirmationToken(): string { return crypto.randomBytes(32).toString('hex'); } export async function sendEmailConfirmation( email: string, name: string, token: string ): Promise { const baseUrl = process.env.NEXTAUTH_URL || process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3001'; const confirmationUrl = `${baseUrl}/api/auth/verify-email?token=${token}`; try { await resend.emails.send({ from: process.env.RESEND_FROM_EMAIL || 'onboarding@resend.dev', to: email, subject: 'Confirm your email address', html: ` Confirm your email

Confirm your email address

Hi ${name},

Thank you for signing up! Please confirm your email address by clicking the button below:

Confirm Email Address

Or copy and paste this link into your browser:

${confirmationUrl}

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

`, }); } catch (error) { console.error('Error sending confirmation email:', error); throw new Error('Failed to send confirmation email'); } } export async function sendEmailConfirmationResend( email: string, name: string, token: string ): Promise { const baseUrl = process.env.NEXTAUTH_URL || process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3001'; const confirmationUrl = `${baseUrl}/api/auth/verify-email?token=${token}`; try { await resend.emails.send({ from: process.env.RESEND_FROM_EMAIL || 'onboarding@resend.dev', to: email, subject: 'Confirm your email address', html: ` Confirm your email

Confirm your email address

Hi ${name},

You requested a new confirmation email. Please confirm your email address by clicking the button below:

Confirm Email Address

Or copy and paste this link into your browser:

${confirmationUrl}

This link will expire in 24 hours. If you didn't request this email, you can safely ignore it.

`, }); } catch (error) { console.error('Error sending confirmation email:', error); throw new Error('Failed to send confirmation email'); } } export function generatePasswordResetToken(): string { return crypto.randomBytes(32).toString('hex'); } export async function sendPasswordResetEmail( email: string, name: string, token: string ): Promise { const baseUrl = process.env.NEXTAUTH_URL || process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3001'; const resetUrl = `${baseUrl}/reset-password?token=${token}`; const fromEmail = process.env.RESEND_FROM_EMAIL || 'onboarding@resend.dev'; const replyTo = process.env.RESEND_REPLY_TO || fromEmail; // Plain text version for better deliverability const text = `Hi ${name}, You requested 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. Best regards, PunimTag Viewer Team`; try { console.log('[EMAIL] Sending password reset email:', { from: fromEmail, to: email, replyTo: replyTo, }); const result = await resend.emails.send({ from: fromEmail, to: email, replyTo: replyTo, subject: 'Reset your password - PunimTag Viewer', text: text, html: ` Reset your password

Reset your password

Hi ${name},

You requested to reset your password for your PunimTag Viewer account. Click the button below to create a new password:

Reset Password

Or copy and paste this link into your browser:

${resetUrl}

Important: This link will expire in 1 hour for security reasons.

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

This is an automated message from PunimTag Viewer. Please do not reply to this email.

`, }); if (result.error) { console.error('[EMAIL] Resend API error:', result.error); throw new Error(`Resend API error: ${result.error.message || 'Unknown error'}`); } console.log('[EMAIL] Password reset email sent successfully:', { emailId: result.data?.id, to: email, }); } catch (error: any) { console.error('[EMAIL] Error sending password reset email:', error); console.error('[EMAIL] Error details:', { message: error?.message, name: error?.name, response: error?.response, statusCode: error?.statusCode, }); throw error; } }