TofuStack/src/lib/server/api/services/mailer.service.ts

44 lines
1.1 KiB
TypeScript
Raw Normal View History

import { injectable } from 'tsyringe';
2024-08-07 15:13:36 +00:00
import type { Email } from '../common/inferfaces/email.interface';
2024-09-02 04:36:41 +00:00
import { config } from '../common/config';
2024-08-05 12:22:46 +00:00
type SendProps = {
2024-05-25 06:02:26 +00:00
to: string | string[];
2024-08-05 12:22:46 +00:00
email: Email;
}
2024-05-25 06:02:26 +00:00
@injectable()
export class MailerService {
2024-08-07 15:13:36 +00:00
async send(data: SendProps) {
2024-09-02 04:36:41 +00:00
const mailer = config.isProduction ? this.sendProd : this.sendDev;
2024-08-07 15:13:36 +00:00
await mailer(data);
}
2024-08-05 12:22:46 +00:00
private async sendDev({ to, email }: SendProps) {
2024-08-07 15:13:36 +00:00
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
Attachments: [],
From: { Email: "noreply@tofustack.com", Name: "TofuStack" },
HTML: email.html(),
Subject: email.subject(),
Text: email.html(),
To: Array.isArray(to) ? to.map(to => ({ Email: to, Name: to })) : [{ Email: to, Name: to }],
})
};
const response = await fetch('http://localhost:8025/api/v1/send', options)
const data = await response.json()
console.log(`http://localhost:8025/view/${data.ID}`)
2024-05-25 06:02:26 +00:00
}
2024-08-05 12:22:46 +00:00
private async sendProd({ to, email }: SendProps) {
// CONFIGURE MAILER
}
2024-05-25 06:02:26 +00:00
}