import nodemailer from 'nodemailer'; import { originStorage } from '../utils/origin-storage'; export class MailService { private transporter: nodemailer.Transporter; constructor() { const host = process.env.SMTP_HOST || 'smtp.mailtrap.io'; const port = parseInt(process.env.SMTP_PORT || '2525', 10); const user = process.env.SMTP_USER || ''; const pass = process.env.SMTP_PASS || ''; this.transporter = nodemailer.createTransport({ host, port, secure: port === 465, auth: user && pass ? { user, pass } : undefined, tls: { rejectUnauthorized: false, }, }); } public async sendInviteEmail(email: string, inviteToken: string) { const origin = originStorage.getStore() || process.env.CLIENT_ORIGIN || 'http://localhost:5173'; const inviteUrl = `${origin}/invite?token=${inviteToken}`; const isRealEmailAllowed = process.env.ENABLE_REAL_EMAILS === 'true' && process.env.NODE_ENV === 'production'; if (!isRealEmailAllowed) { console.log(`[DEV EMAIL SAFEGUARD] Blocked real email dispatch to real user/client: ${email}`); console.log(`[DEV EMAIL SAFEGUARD] Mock Invite URL: ${inviteUrl}`); return { messageId: 'mock-dev-safeguard-id' }; } const mailOptions = { from: process.env.SMTP_FROM || (process.env.SMTP_USER ? `"Tech4Biz Portal" <${process.env.SMTP_USER}>` : '"Tech4Biz Portal" '), to: email, subject: 'Tech4Biz Partner Portal Invitation', html: `

Tech4Biz Partner Portal

Authorized Collaborative Access Gateway

Hello,

You have been invited to register for the Tech4Biz Partner Portal as a collaborative member. This portal allows your organization to view shared resources, asset files, and manage onboarding documentation.

To set up your account and access the platform, please proceed to the link below to configure your credentials and review the partner documentation:

Access Partner Portal

If you are unable to click the button above, please copy and paste the URL below into your web browser:

${inviteUrl}

© 2026 Tech4Biz Solutions Inc. All rights reserved.

` }; try { const info = await this.transporter.sendMail(mailOptions); console.log(`[SMTP] Invitation email successfully sent to ${email}. Message ID: ${info.messageId}`); return info; } catch (err) { console.error(`[SMTP ERROR] Failed to send invitation email to ${email}:`, err); throw err; } } public async sendCustomAnnouncement(options: { recipients: string[]; subject: string; messageBody: string }) { if (!options.recipients || options.recipients.length === 0) return; const isRealEmailAllowed = process.env.ENABLE_REAL_EMAILS === 'true' && process.env.NODE_ENV === 'production'; if (!isRealEmailAllowed) { console.log(`[DEV EMAIL SAFEGUARD] Blocked announcement email to ${options.recipients.length} recipients (Development mode safety guard). Subject: "${options.subject}"`); return; } const mailOptions = { from: process.env.SMTP_FROM || '"Tech4Biz Portal" ', to: options.recipients.join(', '), subject: options.subject, html: `

${options.subject}

${options.messageBody}

Sent via Tech4Biz Channel Partner Platform

` }; try { await this.transporter.sendMail(mailOptions); console.log(`[SMTP] Announcement email sent to ${options.recipients.length} recipients.`); } catch (err) { console.error('[SMTP ERROR] Failed to send announcement email:', err); } } }