From 2814d5520623efe5f48c26f639d3ed6cc5f0d8d2 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Mon, 15 Dec 2025 20:17:22 -0800 Subject: Add email integration --- src/integrations/email.ts | 69 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/integrations/email.ts (limited to 'src/integrations/email.ts') diff --git a/src/integrations/email.ts b/src/integrations/email.ts new file mode 100644 index 0000000..aa4c36c --- /dev/null +++ b/src/integrations/email.ts @@ -0,0 +1,69 @@ +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:

+
    + ${request.files.map(f => `
  • ${f.originalFilename} (${f.contentType}, ${f.size} bytes)
  • `).join('')} +
+ `; + } + + const mailOptions = { + from: config.from, + to: config.to, + subject: subject, + html: htmlBody, + }; + + await transporter.sendMail(mailOptions); + + return undefined; + }); +} -- cgit v1.2.3-70-g09d2