import nodemailer from 'nodemailer'; import '@std/dotenv/load'; 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}"`); } } /** Based off of https://github.com/ActiveCampaign/postmark-templates/tree/main/templates-inlined/basic/password-reset */ private static getHtmlBody(title: string, htmlBody: string) { return `
|
You or someone who knows your email is trying to verify it in bewCloud.
Here's the verification code:
|
|
This code will expire in 30 minutes.
`, ); await this.send(email, emailTitle, htmlBody, textBody); } static async sendLoginVerificationEmail( email: string, verificationCode: string, ) { const emailTitle = 'Verify your login in bewCloud'; const textBody = ` ${emailTitle} ------------------------ You or someone who knows your email and password is trying to login to bewCloud. Here's the verification code: **${verificationCode}** =============================== This code will expire in 30 minutes. `; const htmlBody = this.getHtmlBody( emailTitle, `You or someone who knows your email and password is trying to login to bewCloud.
Here's the verification code:
|
|
This code will expire in 30 minutes.
`, ); await this.send(email, emailTitle, htmlBody, textBody); } }