|
1 |
| -// import { SES } from '@aws-sdk/client-ses' |
2 |
| -// import { log } from '@stacksjs/logging' |
3 |
| -// import type { RenderOptions } from './template' |
4 |
| -// import { template } from './template' |
5 |
| -// import type { Message, SendEmailParams } from './types' |
6 |
| - |
7 |
| -// export class Email { |
8 |
| -// private client: SES |
9 |
| - |
10 |
| -// constructor(private message: Message) { |
11 |
| -// this.client = new SES({ region: 'us-east-1' }) |
12 |
| -// this.message = message |
13 |
| -// } |
14 |
| - |
15 |
| -// public async send(options?: RenderOptions): Promise<{ message: string }> { |
16 |
| -// log.info('Sending email...') |
17 |
| -// const path = this.message.template |
18 |
| - |
19 |
| -// try { |
20 |
| -// const templ = await template(path, options) |
21 |
| - |
22 |
| -// const params: SendEmailParams = { |
23 |
| -// Source: this.message.from?.address || '', |
24 |
| - |
25 |
| -// Destination: { |
26 |
| -// ToAddresses: [this.message.to], |
27 |
| -// }, |
28 |
| - |
29 |
| -// Message: { |
30 |
| -// Body: { |
31 |
| -// Html: { |
32 |
| -// Charset: 'UTF-8', |
33 |
| -// Data: templ.html, |
34 |
| -// }, |
35 |
| -// }, |
36 |
| - |
37 |
| -// Subject: { |
38 |
| -// Charset: 'UTF-8', |
39 |
| -// Data: this.message.subject, |
40 |
| -// }, |
41 |
| -// }, |
42 |
| -// } |
43 |
| - |
44 |
| -// await this.client.sendEmail(params) |
45 |
| - |
46 |
| -// let returnMsg: { message: string } = { message: 'Email sent' } |
47 |
| - |
48 |
| -// if (this.message.handle) returnMsg = await this.message.handle() |
49 |
| - |
50 |
| -// await this.onSuccess() |
51 |
| - |
52 |
| -// return returnMsg |
53 |
| -// } catch (error) { |
54 |
| -// return this.onError(error as Error) |
55 |
| -// } |
56 |
| -// } |
57 |
| - |
58 |
| -// public async onError(error: Error) { |
59 |
| -// log.error(error) |
60 |
| - |
61 |
| -// if (!this.message.onError) return |
62 |
| - |
63 |
| -// return await this.message.onError(error) |
64 |
| -// } |
65 |
| - |
66 |
| -// // public onSuccess() { |
67 |
| -// // try { |
68 |
| -// // if (!this.message.onSuccess) return |
69 |
| - |
70 |
| -// // this.message.onSuccess() |
71 |
| -// // } catch (error) { |
72 |
| -// // return this.onError(error as Error) |
73 |
| -// // } |
74 |
| -// // } |
75 |
| -// } |
76 |
| - |
77 |
| -// export type { Message } |
78 |
| - |
79 |
| -// // export const email = (options: Message) => new Email(options) |
80 |
| -// export default Email |
| 1 | +import type { RenderOptions } from './template' |
| 2 | +import type { Message, SendEmailParams } from './types' |
| 3 | +import { SES } from '@aws-sdk/client-ses' |
| 4 | +import { log } from '@stacksjs/logging' |
| 5 | +import { template } from './template' |
| 6 | + |
| 7 | +export class Email { |
| 8 | + private client: SES |
| 9 | + |
| 10 | + constructor(private message: Message) { |
| 11 | + this.client = new SES({ region: 'us-east-1' }) |
| 12 | + this.message = message |
| 13 | + } |
| 14 | + |
| 15 | + public async send(options?: RenderOptions): Promise<{ message: string }> { |
| 16 | + log.info('Sending email...') |
| 17 | + const path = this.message.template |
| 18 | + |
| 19 | + try { |
| 20 | + const templ = await template(path, options) |
| 21 | + |
| 22 | + const params: SendEmailParams = { |
| 23 | + Source: this.message.from?.address || '', |
| 24 | + |
| 25 | + Destination: { |
| 26 | + ToAddresses: [this.message.to], |
| 27 | + }, |
| 28 | + |
| 29 | + Message: { |
| 30 | + Body: { |
| 31 | + Html: { |
| 32 | + Charset: 'UTF-8', |
| 33 | + Data: templ, |
| 34 | + }, |
| 35 | + }, |
| 36 | + |
| 37 | + Subject: { |
| 38 | + Charset: 'UTF-8', |
| 39 | + Data: this.message.subject, |
| 40 | + }, |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + await this.client.sendEmail(params) |
| 45 | + |
| 46 | + let returnMsg: { message: string } = { message: 'Email sent' } |
| 47 | + |
| 48 | + if (this.message.handle) |
| 49 | + returnMsg = await this.message.handle() |
| 50 | + |
| 51 | + await this.onSuccess() |
| 52 | + |
| 53 | + return returnMsg |
| 54 | + } |
| 55 | + catch (error) { |
| 56 | + return this.onError(error as Error) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public async onError(error: Error) { |
| 61 | + log.error(error) |
| 62 | + |
| 63 | + if (!this.message.onError) |
| 64 | + return |
| 65 | + |
| 66 | + return await this.message.onError(error) |
| 67 | + } |
| 68 | + |
| 69 | + public onSuccess() { |
| 70 | + try { |
| 71 | + if (!this.message.onSuccess) |
| 72 | + return |
| 73 | + |
| 74 | + this.message.onSuccess() |
| 75 | + } |
| 76 | + catch (error) { |
| 77 | + return this.onError(error as Error) |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +export type { Message } |
| 83 | + |
| 84 | +// export const email = (options: Message) => new Email(options) |
| 85 | +export default Email |
0 commit comments