make the text pretty

This commit is contained in:
Elizabeth Hunt 2024-01-07 01:32:15 -07:00
parent db671d26c4
commit b09e1e6032
Signed by: simponic
GPG Key ID: 52B3774857EB24B1
3 changed files with 56 additions and 70 deletions

View File

@ -1,3 +1,2 @@
export * from './setImageUrl'; export * from './setImageUrl';
export * from './mailer';
export * from './retry'; export * from './retry';

View File

@ -1,61 +0,0 @@
import * as nodemailer from 'nodemailer';
import { continueRetryUntilValidation } from './retry';
export interface Mailer {
sendMail(to: string, subject: string, message: string): Promise<boolean>;
}
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<string>(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)
);
};

View File

@ -1,10 +1,54 @@
import 'dotenv/config'; import 'dotenv/config';
import { EnvMistyMountainsMailerFactory } from '$lib/utils'; import * as nodemailer from 'nodemailer';
import { continueRetryUntilValidation } from '$lib/utils';
class MistyMountainsMailer {
constructor(username, password, from, domain, port) {
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
}
});
}
async sendMail(to, subject, message) {
const mail = {
from: this.from,
subject,
html: message,
to
};
return !!(await continueRetryUntilValidation(async () => {
const { messageId } = await this.transporter.sendMail(mail);
return messageId;
}));
}
}
export async function post({ request }) { export async function post({ request }) {
const body = await request.json(); const body = await request.json();
const { HCAPTCHA_SECRET, FORM_TO_EMAIL } = process.env; const { HCAPTCHA_SECRET, FORM_TO_EMAIL } = process.env;
const mailer = EnvMistyMountainsMailerFactory(); const mailer = new MistyMountainsMailer(
process.env.SMTP_USERNAME,
process.env.SMTP_PASSWORD,
process.env.FROM_EMAIL,
process.env.SMTP_SERVER,
Number(process.env.SMTP_PORT)
);
const captchaVerified = await fetch( const captchaVerified = await fetch(
`https://hcaptcha.com/siteverify?response=${body.captchaToken}&secret=${HCAPTCHA_SECRET}`, `https://hcaptcha.com/siteverify?response=${body.captchaToken}&secret=${HCAPTCHA_SECRET}`,
@ -25,14 +69,18 @@ export async function post({ request }) {
}; };
} }
const text = `Name: ${body.name} const text = `<h1>new MMT message</h1>
Phone Number: ${body.phone || 'Not Given'} <p>Name: ${body.name}</p>
Email: ${body.email} <p>Phone Number: ${body.phone || 'Not Given'}</p>
Message: ${body.message} <p>Email: ${body.email}</p>
`; <hr>
<br>
<p>${body.message}</p>
<br>
<p>brought to you by <a href="https://github.com/Simponic/mistymountains">simponic</a></p>`;
const messageSent = await mailer const messageSent = await mailer
.sendMail(FORM_TO_EMAIL, `Form Submission from ${body.name}`, text) .sendMail(FORM_TO_EMAIL, `New Message From ${body.name}`, text)
.then(() => true) .then(() => true)
.catch((error) => { .catch((error) => {
console.error(error); console.error(error);