--- name: communication-systems description: Email, notifications, and messaging system patterns domain: domain-applications version: 1.0.0 tags: [email, notifications, push, webhooks, messaging] --- # Communication Systems ## Overview Building email systems, push notifications, in-app messaging, and webhook integrations. --- ## Email Systems ### Transactional Email ```typescript import { Resend } from 'resend'; const resend = new Resend(process.env.RESEND_API_KEY); interface EmailOptions { to: string | string[]; subject: string; html?: string; text?: string; template?: string; data?: Record; attachments?: Array<{ filename: string; content: Buffer | string; }>; } async function sendEmail(options: EmailOptions) { let html = options.html; // Use template if specified if (options.template) { html = await renderTemplate(options.template, options.data); } const { data, error } = await resend.emails.send({ from: 'noreply@example.com', to: options.to, subject: options.subject, html, text: options.text, attachments: options.attachments, }); if (error) { console.error('Email send failed:', error); throw error; } // Log for tracking await prisma.emailLog.create({ data: { messageId: data.id, to: Array.isArray(options.to) ? options.to.join(',') : options.to, subject: options.subject, template: options.template, status: 'sent', }, }); return data; } // Email templates with React Email import { render } from '@react-email/render'; import { WelcomeEmail } from './templates/WelcomeEmail'; import { PasswordResetEmail } from './templates/PasswordResetEmail'; const templates = { welcome: WelcomeEmail, passwordReset: PasswordResetEmail, }; async function renderTemplate(name: string, data: Record) { const Template = templates[name]; if (!Template) throw new Error(`Template ${name} not found`); return render(