From db671d26c43e47b8285d319a0fd758df60d729c7 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Sun, 7 Jan 2024 01:21:06 -0700 Subject: initial update with nodemailer --- src/lib/utils/mailer.ts | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/lib/utils/mailer.ts (limited to 'src/lib/utils/mailer.ts') diff --git a/src/lib/utils/mailer.ts b/src/lib/utils/mailer.ts new file mode 100644 index 0000000..e5edd74 --- /dev/null +++ b/src/lib/utils/mailer.ts @@ -0,0 +1,61 @@ +import * as nodemailer from 'nodemailer'; +import { continueRetryUntilValidation } from './retry'; + +export interface Mailer { + sendMail(to: string, subject: string, message: string): Promise; +} + +export class MistyMountainsMailer implements Mailer { + private from: string; + private domain: string; + private username: string; + private password: string; + private port: number; + + private transporter: nodemailer.Transporter; + + constructor(username: string, password: string, from: string, domain: string, port: number) { + this.from = from; + this.username = username; + this.password = password; + this.domain = domain; + this.port = port; + + this.transporter = nodemailer.createTransport({ + host: this.domain, + port: this.port, + auth: { + user: this.username, + pass: this.password + }, + requireTLS: true, + tls: { + rejectUnauthorized: true + } + }); + } + + public async sendMail(to: string, subject: string, message: string) { + const mail = { + from: this.from, + subject, + html: message, + to + }; + + return !!(await continueRetryUntilValidation(async () => { + const { messageId } = await this.transporter.sendMail(mail); + return messageId; + })); + } +} + +export const EnvMistyMountainsMailerFactory = () => { + return new MistyMountainsMailer( + process.env.SMTP_USERNAME, + process.env.SMTP_PASSWORD, + process.env.FROM_EMAIL, + process.env.SMTP_SERVER, + Number(process.env.SMTP_PORT) + ); +}; -- cgit v1.2.3-70-g09d2