@stacksjs/email 0.58.72 → 0.59.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/email.ts ADDED
@@ -0,0 +1,84 @@
1
+ import { SES } from '@aws-sdk/client-ses'
2
+ import type { RenderOptions } from './template'
3
+ import { template } from './template'
4
+ import type { Message, SendEmailParams } from './types'
5
+
6
+ export class Email {
7
+ private client: SES
8
+
9
+ constructor(private message: Message) {
10
+ this.client = new SES({ region: 'us-east-1' })
11
+ this.message = message
12
+ }
13
+
14
+ public async send(options?: RenderOptions) {
15
+ log.info('Sending email...')
16
+ const path = this.message.template
17
+
18
+ try {
19
+ const templ = await template(path, options)
20
+
21
+ const params: SendEmailParams = {
22
+ Source: this.message.from?.address || '',
23
+
24
+ Destination: {
25
+ ToAddresses: [this.message.to],
26
+ },
27
+
28
+ Message: {
29
+ Body: {
30
+ Html: {
31
+ Charset: 'UTF-8',
32
+ Data: templ.html,
33
+ },
34
+ },
35
+
36
+ Subject: {
37
+ Charset: 'UTF-8',
38
+ Data: this.message.subject,
39
+ },
40
+ },
41
+ }
42
+
43
+ await this.client.sendEmail(params)
44
+
45
+ let returnMsg: { message: string } = { message: 'Email sent' }
46
+
47
+ if (this.message.handle)
48
+ returnMsg = await this.message.handle()
49
+
50
+ await this.onSuccess()
51
+
52
+ return returnMsg
53
+ }
54
+ catch (error) {
55
+ return this.onError(error as Error)
56
+ }
57
+ }
58
+
59
+ public async onError(error: Error) {
60
+ log.error(error)
61
+
62
+ if (!this.message.onError)
63
+ return
64
+
65
+ return await this.message.onError(error)
66
+ }
67
+
68
+ public onSuccess() {
69
+ try {
70
+ if (!this.message.onSuccess)
71
+ return
72
+
73
+ this.message.onSuccess()
74
+ }
75
+ catch (error) {
76
+ return this.onError(error as Error)
77
+ }
78
+ }
79
+ }
80
+
81
+ export type { Message }
82
+
83
+ export const email = (options: Message) => new Email(options)
84
+ export default Email
package/src/index.ts CHANGED
@@ -1 +1,3 @@
1
1
  export * from './drivers'
2
+ export * from './email.ts'
3
+ export * from './types'
package/src/send.ts CHANGED
@@ -1,36 +1,33 @@
1
- import { italic, log } from '@stacksjs/cli'
1
+ import { italic } from '@stacksjs/cli'
2
2
  import { ResultAsync } from '@stacksjs/error-handling'
3
3
  import type { EmailOptions } from '@stacksjs/types'
4
- import * as Maizzle from '@maizzle/framework'
5
4
 
6
5
  // import { stringify } from 'json5'
7
- import * as maizzleConfig from './utils/config'
8
6
 
9
- export async function send(options: EmailOptions, provider: any, providerName: string, css?: string): Promise<ResultAsync<any, Error>> {
10
- const template = `
11
- <extends src="./src/notifications/src/utils/template.html">
12
- <block name="template">
13
- ${options.html}
14
- </block>
15
- </extends>`
7
+ export async function send(options: EmailOptions, provider: any, providerName: string): Promise<ResultAsync<any, Error>> {
8
+ // const template = `
9
+ // <extends src="./src/notifications/src/utils/template.html">
10
+ // <block name="template">
11
+ // ${options.html}
12
+ // </block>
13
+ // </extends>`
16
14
 
17
- await Maizzle.render(
18
- template,
19
- {
20
- tailwind: {
21
- // config: stringify(config),
22
- css,
23
- maizzle: maizzleConfig,
24
- },
25
- },
26
- )
27
- .then(({ html }: any) => {
28
- options.html = html
29
- })
30
- .catch((error: any) => log.error(`Failed to render email template using provider: ${italic(providerName)}.`, error))
15
+ // await Maizzle.render(
16
+ // template,
17
+ // {
18
+ // tailwind: {
19
+ // // config: stringify(config),
20
+ // css,
21
+ // maizzle: maizzleConfig,
22
+ // },
23
+ // },
24
+ // )
25
+ // .then(({ html }: any) => {
26
+ // options.html = html
27
+ // })
28
+ // .catch((error: any) => log.error(`Failed to render email template using provider: ${italic(providerName)}.`, error))
31
29
 
32
30
  return ResultAsync.fromPromise(
33
-
34
31
  provider.sendMessage(options),
35
32
  () => new Error(`Failed to send message using provider: ${italic(providerName)}`),
36
33
  )