import nodemailer from 'nodemailer'; import type { Transporter } from 'nodemailer'; export interface EmailConfig { host: string; port: number; secure: boolean; auth?: { user: string; pass: string; }; from: string; } export class EmailService { private transporter: Transporter; private config: EmailConfig; constructor(config: EmailConfig) { this.config = config; // For development, use Ethereal Email (fake SMTP service) if (process.env.NODE_ENV === 'development' && !config.auth) { // This will be setup asynchronously this.setupDevelopmentTransporter(); } else { this.transporter = nodemailer.createTransporter({ host: config.host, port: config.port, secure: config.secure, auth: config.auth, }); } } private async setupDevelopmentTransporter() { try { // Create Ethereal Email account for development const testAccount = await nodemailer.createTestAccount(); this.transporter = nodemailer.createTransporter({ host: 'smtp.ethereal.email', port: 587, secure: false, auth: { user: testAccount.user, pass: testAccount.pass, }, }); console.log('Development email transporter created with Ethereal Email'); console.log('Preview URLs will be logged when emails are sent'); } catch (error) { console.error('Failed to create development email transporter:', error); // Fallback to console logging this.transporter = { sendMail: async (options: any) => { console.log('Email would be sent:', options); return { messageId: 'dev-' + Date.now() }; }, } as any; } } async sendEmailVerification(to: string, token: string, username?: string): Promise { const verificationUrl = `${process.env.FRONTEND_URL || 'http://localhost:3000'}/verify-email?token=${token}`; const html = ` Verify your ZephyrFS account

Welcome to ZephyrFS!

Verify Your Email Address

Hello${username ? ` ${username}` : ''}!

Thank you for signing up for ZephyrFS, the secure and decentralized file backup system.

To complete your registration and start using ZephyrFS, please verify your email address by clicking the button below:

Verify Email Address

Or copy and paste this link into your browser:

${verificationUrl}

This verification link will expire in 24 hours.

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

`; const text = ` Welcome to ZephyrFS! Hello${username ? ` ${username}` : ''}! Thank you for signing up for ZephyrFS, the secure and decentralized file backup system. To complete your registration and start using ZephyrFS, please verify your email address by visiting this link: ${verificationUrl} This verification link will expire in 24 hours. If you didn't create an account with ZephyrFS, you can safely ignore this email. © 2024 ZephyrFS. All rights reserved. `; const result = await this.transporter.sendMail({ from: this.config.from || 'ZephyrFS ', to, subject: 'Verify your ZephyrFS account', text, html, }); // In development, log preview URL if (process.env.NODE_ENV === 'development') { const previewUrl = nodemailer.getTestMessageUrl(result); if (previewUrl) { console.log('Email verification preview:', previewUrl); } } } async sendPasswordReset(to: string, token: string, username?: string): Promise { const resetUrl = `${process.env.FRONTEND_URL || 'http://localhost:3000'}/reset-password?token=${token}`; const html = ` Reset your ZephyrFS password

Password Reset Request

Reset Your Password

Hello${username ? ` ${username}` : ''}!

We received a request to reset the password for your ZephyrFS account.

⚠️ Security Notice: If you didn't request this password reset, please ignore this email. Your account is safe.

To reset your password, click the button below:

Or copy and paste this link into your browser:

${resetUrl}

This reset link will expire in 1 hour.

After clicking the link, you'll be able to create a new password for your account.

`; const text = ` Password Reset Request Hello${username ? ` ${username}` : ''}! We received a request to reset the password for your ZephyrFS account. If you didn't request this password reset, please ignore this email. Your account is safe. To reset your password, visit this link: ${resetUrl} This reset link will expire in 1 hour. After clicking the link, you'll be able to create a new password for your account. © 2024 ZephyrFS. All rights reserved. `; const result = await this.transporter.sendMail({ from: this.config.from || 'ZephyrFS Security ', to, subject: 'Reset your ZephyrFS password', text, html, }); // In development, log preview URL if (process.env.NODE_ENV === 'development') { const previewUrl = nodemailer.getTestMessageUrl(result); if (previewUrl) { console.log('Password reset preview:', previewUrl); } } } async sendWelcomeEmail(to: string, userType: 'backup' | 'volunteer', username?: string): Promise { const isVolunteer = userType === 'volunteer'; const dashboardUrl = `${process.env.FRONTEND_URL || 'http://localhost:3000'}/dashboard`; const html = ` Welcome to ZephyrFS!

🎉 Welcome to ZephyrFS!

Your account is ready!

Hello${username ? ` ${username}` : ''}!

Welcome to ZephyrFS! Your email has been verified and your account is now active.

${isVolunteer ? `

🤝 Thank you for volunteering!

As a storage volunteer, you're helping build a decentralized, secure backup network that benefits everyone.

📁 Safe Storage Allocation
Choose how much disk space to contribute safely without risking your system.
🔒 Zero-Knowledge Security
All stored data is encrypted - you can't see what's stored on your system.
💰 Earn Rewards
Get compensated for providing reliable storage to the network.

Ready to get started? Use our desktop app to set up your storage node:

` : `

☁️ Secure Backup Made Simple

Your files will be encrypted, distributed, and safely backed up across our decentralized network.

🔐 Military-Grade Encryption
Your files are encrypted before leaving your device.
🌐 Distributed Storage
Files are split and stored across multiple secure nodes.
⚡ Simple as Google Drive
Drag and drop to backup. That's it!

Ready to start backing up your files?

`}

If you have any questions or need help getting started, our community is here to support you!

`; const result = await this.transporter.sendMail({ from: this.config.from || 'ZephyrFS ', to, subject: '🎉 Welcome to ZephyrFS - Your account is ready!', html, }); // In development, log preview URL if (process.env.NODE_ENV === 'development') { const previewUrl = nodemailer.getTestMessageUrl(result); if (previewUrl) { console.log('Welcome email preview:', previewUrl); } } } }