import { Either, type IEither } from '@emprespresso/pengueno'; import type { EmailConfig, StoredRequest } from '../types/index.js'; import nodemailer from 'nodemailer'; export async function sendEmailNotification(config: EmailConfig, request: StoredRequest): Promise> { if (!config.enabled || !config.to || !config.from) { return Either.right(undefined); } return Either.fromFailableAsync(async () => { // Create transporter based on configuration const transporter = nodemailer.createTransport({ host: config.host || 'localhost', port: config.port || 25, secure: config.secure ?? false, auth: config.username && config.password ? { user: config.username, pass: config.password, } : undefined, }); const subject = config.subject || `Webhook received: ${request.routeName}`; // Build email body let htmlBody = `

Webhook Notification

Route: ${request.routeName}

Method: ${request.method}

Timestamp: ${new Date(request.timestamp).toISOString()}

UUID: ${request.uuid}

`; if (config.includeBody && request.body !== undefined) { htmlBody += `

Request Body:

${JSON.stringify(request.body, null, 2)}
`; } if (config.includeHeaders && request.headers) { htmlBody += `

Headers:

${JSON.stringify(request.headers, null, 2)}
`; } if (request.files && request.files.length > 0) { htmlBody += `

Uploaded Files:

`; } const mailOptions = { from: config.from, to: config.to, subject: subject, html: htmlBody, }; await transporter.sendMail(mailOptions); return undefined; }); }