// deno-fmt-ignore-file import nodemailer from 'nodemailer'; import 'std/dotenv/load.ts'; import { escapeHtml } from '/lib/utils/misc.ts'; import { AppConfig } from '/lib/config.ts'; const SMTP_USERNAME = Deno.env.get('SMTP_USERNAME') || ''; const SMTP_PASSWORD = Deno.env.get('SMTP_PASSWORD') || ''; export class EmailModel { private static async send(to: string, subject: string, htmlBody: string, textBody: string) { const emailConfig = await AppConfig.getEmailConfig(); if (!emailConfig.from || !emailConfig.host || !emailConfig.port) { throw new Error('config.email.from, config.email.host, or config.email.port is not set'); } const transporterConfig = { host: emailConfig.host, port: emailConfig.port, secure: Number(emailConfig.port) === 465, auth: { user: SMTP_USERNAME, pass: SMTP_PASSWORD, }, }; const transporter = nodemailer.createTransport(transporterConfig); const mailOptions = { from: emailConfig.from, to, subject, html: htmlBody, text: textBody, }; try { await transporter.sendMail(mailOptions); console.log(`Email sent to "${to}", "${subject}"`); } catch (error) { console.log(error); throw new Error(`Failed to send email to "${to}", "${subject}"`); } } static async sendVerificationEmail( email: string, verificationCode: string, ) { const emailTitle = 'Verify your email in bewCloud'; const textBody = ` ${emailTitle} ------------------------ You or someone who knows your email is trying to verify it in bewCloud. Here's the verification code: **${verificationCode}** =============================== This code will expire in 30 minutes. `; /** Based off of https://github.com/ActiveCampaign/postmark-templates/tree/main/templates-inlined/basic/password-reset */ const htmlBody = `
|