@stacksjs/email 0.66.0 → 0.67.0
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/dist/index.js +0 -3
- package/package.json +7 -9
- package/dist/index.js.map +0 -9
- package/src/drivers/index.ts +0 -3
- package/src/drivers/mailgun.ts +0 -21
- package/src/drivers/nodemailer.ts +0 -23
- package/src/drivers/ses.ts +0 -23
- package/src/email.ts +0 -80
- package/src/index.ts +0 -3
- package/src/send.ts +0 -38
- package/src/server/converter.ts +0 -302
- package/src/server/inbound.ts +0 -404
- package/src/server/outbound.ts +0 -296
- package/src/template.ts +0 -20
- package/src/types.ts +0 -39
- package/src/utils/config.ts +0 -5
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/email",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.67.0",
|
|
5
5
|
"description": "The Stacks Email integration. Painlessly create & manage your inboxes, templates, and send emails.",
|
|
6
6
|
"author": "Chris Breuer",
|
|
7
7
|
"contributors": ["Chris Breuer <chris@stacksjs.org>"],
|
|
@@ -19,17 +19,15 @@
|
|
|
19
19
|
"keywords": ["email", "ses", "aws", "stacks", "emailjs", "mailgun"],
|
|
20
20
|
"exports": {
|
|
21
21
|
".": {
|
|
22
|
-
"bun": "./src/index.ts",
|
|
23
22
|
"import": "./dist/index.js"
|
|
24
23
|
},
|
|
25
24
|
"./*": {
|
|
26
|
-
"bun": "./src/*",
|
|
27
25
|
"import": "./dist/*"
|
|
28
26
|
}
|
|
29
27
|
},
|
|
30
28
|
"module": "dist/index.js",
|
|
31
29
|
"types": "dist/index.d.ts",
|
|
32
|
-
"files": ["README.md", "dist"
|
|
30
|
+
"files": ["README.md", "dist"],
|
|
33
31
|
"scripts": {
|
|
34
32
|
"build": "bun build.ts",
|
|
35
33
|
"build:inbound": "bun build-inbound.ts",
|
|
@@ -37,14 +35,14 @@
|
|
|
37
35
|
"prepublishOnly": "bun run build"
|
|
38
36
|
},
|
|
39
37
|
"dependencies": {
|
|
40
|
-
"@stacksjs/cli": "0.
|
|
41
|
-
"@stacksjs/config": "0.
|
|
42
|
-
"@stacksjs/error-handling": "0.
|
|
43
|
-
"@stacksjs/types": "0.
|
|
38
|
+
"@stacksjs/cli": "0.67.0",
|
|
39
|
+
"@stacksjs/config": "0.67.0",
|
|
40
|
+
"@stacksjs/error-handling": "0.67.0",
|
|
41
|
+
"@stacksjs/types": "0.67.0",
|
|
44
42
|
"aws-sdk": "^2.1691.0",
|
|
45
43
|
"vue-email": "^0.8.11"
|
|
46
44
|
},
|
|
47
45
|
"devDependencies": {
|
|
48
|
-
"@stacksjs/development": "0.
|
|
46
|
+
"@stacksjs/development": "0.67.0"
|
|
49
47
|
}
|
|
50
48
|
}
|
package/dist/index.js.map
DELETED
package/src/drivers/index.ts
DELETED
package/src/drivers/mailgun.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
// import { MailgunEmailProvider } from '@novu/mailgun'
|
|
2
|
-
// import type { EmailOptions } from '@stacksjs/types'
|
|
3
|
-
// import type { ResultAsync } from '@stacksjs/error-handling'
|
|
4
|
-
// import { notification } from '@stacksjs/config'
|
|
5
|
-
// import { send as sendEmail } from '../send'
|
|
6
|
-
//
|
|
7
|
-
// const env = notification.email
|
|
8
|
-
// const service = notification.email?.drivers.mailgun
|
|
9
|
-
//
|
|
10
|
-
// const provider = new MailgunEmailProvider({
|
|
11
|
-
// apiKey: service?.key || '',
|
|
12
|
-
// domain: service?.domain || '',
|
|
13
|
-
// username: service?.username || '',
|
|
14
|
-
// from: env?.from.address || '',
|
|
15
|
-
// })
|
|
16
|
-
//
|
|
17
|
-
// async function send(options: EmailOptions, css?: string): Promise<ResultAsync<any, Error>> {
|
|
18
|
-
// return sendEmail(options, provider, 'Mailgun', css)
|
|
19
|
-
// }
|
|
20
|
-
//
|
|
21
|
-
// export { send as Send, send }
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
// import { NodemailerProvider } from '@novu/nodemailer'
|
|
2
|
-
// import type { EmailOptions } from '@stacksjs/types'
|
|
3
|
-
// import type { ResultAsync } from '@stacksjs/error-handling'
|
|
4
|
-
// import { notification } from '@stacksjs/config'
|
|
5
|
-
// import { send as sendEmail } from '../send'
|
|
6
|
-
//
|
|
7
|
-
// const env = notification.email
|
|
8
|
-
// const service = notification.email?.drivers.nodemailer
|
|
9
|
-
//
|
|
10
|
-
// const provider = new NodemailerProvider({
|
|
11
|
-
// from: env?.from.address || '',
|
|
12
|
-
// host: service?.host || '',
|
|
13
|
-
// user: service?.user,
|
|
14
|
-
// password: service?.password,
|
|
15
|
-
// port: service?.port || 587,
|
|
16
|
-
// secure: service?.secure,
|
|
17
|
-
// })
|
|
18
|
-
//
|
|
19
|
-
// async function send(options: EmailOptions, css?: string): Promise<ResultAsync<any, Error>> {
|
|
20
|
-
// return sendEmail(options, provider, 'Nodemailer', css)
|
|
21
|
-
// }
|
|
22
|
-
//
|
|
23
|
-
// export { send as Send, send }
|
package/src/drivers/ses.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
// import { SESEmailProvider } from '@novu/ses'
|
|
2
|
-
// import type { EmailOptions } from '@stacksjs/types'
|
|
3
|
-
// import { notification } from '@stacksjs/config'
|
|
4
|
-
// import type { ResultAsync } from '@stacksjs/error-handling'
|
|
5
|
-
// import { send as sendEmail } from '../send'
|
|
6
|
-
|
|
7
|
-
// const email = notification.email
|
|
8
|
-
// const env = email?.drivers?.ses
|
|
9
|
-
|
|
10
|
-
// // const provider = new SESEmailProvider({
|
|
11
|
-
// const provider = new SESEmailProvider({
|
|
12
|
-
// region: env?.region || 'us-east-1',
|
|
13
|
-
// accessKeyId: env?.key || '',
|
|
14
|
-
// secretAccessKey: env?.secret || '',
|
|
15
|
-
// senderName: email?.from.name || 'Stacks',
|
|
16
|
-
// from: email?.from.address || '',
|
|
17
|
-
// })
|
|
18
|
-
|
|
19
|
-
// async function send(options: EmailOptions, css?: string): Promise<ResultAsync<any, Error>> {
|
|
20
|
-
// return sendEmail(options, provider, 'Ses', css)
|
|
21
|
-
// }
|
|
22
|
-
|
|
23
|
-
// export { send as Send, send }
|
package/src/email.ts
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
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
|
package/src/index.ts
DELETED
package/src/send.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
// import { italic } from '@stacksjs/cli'
|
|
2
|
-
// import { ResultAsync } from '@stacksjs/error-handling'
|
|
3
|
-
// import type { EmailOptions } from '@stacksjs/types'
|
|
4
|
-
|
|
5
|
-
// // import { stringify } from 'json5'
|
|
6
|
-
|
|
7
|
-
// export async function send(
|
|
8
|
-
// options: EmailOptions,
|
|
9
|
-
// provider: any,
|
|
10
|
-
// providerName: string,
|
|
11
|
-
// ): Promise<ResultAsync<any, Error>> {
|
|
12
|
-
// // const template = `
|
|
13
|
-
// // <extends src="./src/notifications/src/utils/template.html">
|
|
14
|
-
// // <block name="template">
|
|
15
|
-
// // ${options.html}
|
|
16
|
-
// // </block>
|
|
17
|
-
// // </extends>`
|
|
18
|
-
|
|
19
|
-
// // await Maizzle.render(
|
|
20
|
-
// // template,
|
|
21
|
-
// // {
|
|
22
|
-
// // tailwind: {
|
|
23
|
-
// // // config: stringify(config),
|
|
24
|
-
// // css,
|
|
25
|
-
// // maizzle: maizzleConfig,
|
|
26
|
-
// // },
|
|
27
|
-
// // },
|
|
28
|
-
// // )
|
|
29
|
-
// // .then(({ html }: any) => {
|
|
30
|
-
// // options.html = html
|
|
31
|
-
// // })
|
|
32
|
-
// // .catch((error: any) => log.error(`Failed to render email template using provider: ${italic(providerName)}.`, error))
|
|
33
|
-
|
|
34
|
-
// return ResultAsync.fromPromise(
|
|
35
|
-
// provider.sendMessage(options),
|
|
36
|
-
// () => new Error(`Failed to send message using provider: ${italic(providerName)}`),
|
|
37
|
-
// )
|
|
38
|
-
// }
|
package/src/server/converter.ts
DELETED
|
@@ -1,302 +0,0 @@
|
|
|
1
|
-
// const AWS = require('aws-sdk')
|
|
2
|
-
// const mime = require('mime')
|
|
3
|
-
// const parser = require('mailparser').simpleParser
|
|
4
|
-
//
|
|
5
|
-
// // Initialize S3.
|
|
6
|
-
// const s3 = new AWS.S3({
|
|
7
|
-
// apiVersion: '2006-03-01',
|
|
8
|
-
// })
|
|
9
|
-
//
|
|
10
|
-
// // This lambda will read RAW emails and convert them in easy to read formats
|
|
11
|
-
// // like: HTML and text
|
|
12
|
-
// exports.handler = (event: any) => {
|
|
13
|
-
// // 1. We need to process the path received by S3 since AWS dose escape the
|
|
14
|
-
// // string in a special way. They escape the string in a HTML style
|
|
15
|
-
// // but for whatever reason they convert spaces in to +ses.
|
|
16
|
-
// const s3_key = event.Records[0].s3.object.key
|
|
17
|
-
//
|
|
18
|
-
// // 2. So first we convert the + in to spaces
|
|
19
|
-
// const plus_to_space = s3_key.replace(/\+/g, ' ')
|
|
20
|
-
//
|
|
21
|
-
// // 3. And then we unescape the string, other wise we lose real + characters
|
|
22
|
-
// const unescaped_key = decodeURIComponent(plus_to_space)
|
|
23
|
-
//
|
|
24
|
-
// // 4. This JS object will contain all the data within the chain
|
|
25
|
-
// const container = {
|
|
26
|
-
// bucket: event.Records[0].s3.bucket.name,
|
|
27
|
-
// key: unescaped_key,
|
|
28
|
-
// parsed: {
|
|
29
|
-
// html: '',
|
|
30
|
-
// text: '',
|
|
31
|
-
// attachments: [],
|
|
32
|
-
// },
|
|
33
|
-
// }
|
|
34
|
-
//
|
|
35
|
-
// // -> Start the chain.
|
|
36
|
-
// load_the_email(container)
|
|
37
|
-
// .then((container) => {
|
|
38
|
-
// return remove_extension(container)
|
|
39
|
-
// }).then((container) => {
|
|
40
|
-
// return parse_the_email(container)
|
|
41
|
-
// }).then((container) => {
|
|
42
|
-
// return save_text(container)
|
|
43
|
-
// }).then((container) => {
|
|
44
|
-
// return save_html(container)
|
|
45
|
-
// }).then((container) => {
|
|
46
|
-
// return save_attachments(container)
|
|
47
|
-
// }).then((container) => {
|
|
48
|
-
// return true
|
|
49
|
-
// }).catch((error) => {
|
|
50
|
-
// console.error(error)
|
|
51
|
-
//
|
|
52
|
-
// return false
|
|
53
|
-
// })
|
|
54
|
-
// }
|
|
55
|
-
//
|
|
56
|
-
// // Load the email that triggered the function from S3
|
|
57
|
-
// function load_the_email(container: any) {
|
|
58
|
-
// return new Promise((resolve, reject) => {
|
|
59
|
-
// console.info('load_the_email')
|
|
60
|
-
//
|
|
61
|
-
// // 1. Set the query.
|
|
62
|
-
// const params = {
|
|
63
|
-
// Bucket: container.bucket,
|
|
64
|
-
// Key: container.key,
|
|
65
|
-
// }
|
|
66
|
-
//
|
|
67
|
-
// // -> Execute the query
|
|
68
|
-
// s3.getObject(params, (error: any, data: any) => {
|
|
69
|
-
// // 1. Check for internal errors
|
|
70
|
-
// if (error)
|
|
71
|
-
// return reject(error)
|
|
72
|
-
//
|
|
73
|
-
// // 2. Save the email for the next promise
|
|
74
|
-
// container.raw_email = data.Body
|
|
75
|
-
//
|
|
76
|
-
// // -> Move to the next chain.
|
|
77
|
-
// return resolve(container)
|
|
78
|
-
// })
|
|
79
|
-
// })
|
|
80
|
-
// }
|
|
81
|
-
//
|
|
82
|
-
// // Since the raw email is saved with an extension we need to remove it
|
|
83
|
-
// // before we can save other converted versions.
|
|
84
|
-
// function remove_extension(container: any) {
|
|
85
|
-
// return new Promise((resolve, reject) => {
|
|
86
|
-
// console.info('remove_extension')
|
|
87
|
-
//
|
|
88
|
-
// // 1. Split the string.
|
|
89
|
-
// const tmp = container.key.split('.')
|
|
90
|
-
//
|
|
91
|
-
// // 2. Remove the last element from the array which is `eml`.
|
|
92
|
-
// tmp.pop()
|
|
93
|
-
//
|
|
94
|
-
// // 2. Join the array back to how it was, minus the extension.
|
|
95
|
-
// // We need to do this, since we don't know how many dots
|
|
96
|
-
// // were in the email title for example.
|
|
97
|
-
// container.key = tmp.join('.')
|
|
98
|
-
//
|
|
99
|
-
// // -> Move to the next chain.
|
|
100
|
-
// return resolve(container)
|
|
101
|
-
// })
|
|
102
|
-
// }
|
|
103
|
-
//
|
|
104
|
-
// // Convert the raw email in to HTML and Text, and extract also the attachments
|
|
105
|
-
// function parse_the_email(container: any) {
|
|
106
|
-
// return new Promise((resolve, reject) => {
|
|
107
|
-
// // 1. Parse the email and extract all the necessary
|
|
108
|
-
// parser(container.raw_email, (error: any, parsed: any) => {
|
|
109
|
-
// // 1. Check for internal errors
|
|
110
|
-
// if (error)
|
|
111
|
-
// return reject(error)
|
|
112
|
-
//
|
|
113
|
-
// // 2. Save the parsed email for the next promise.
|
|
114
|
-
// container.parsed.html = parsed.html
|
|
115
|
-
// container.parsed.text = parsed.text
|
|
116
|
-
// container.parsed.attachments = parsed.attachments
|
|
117
|
-
//
|
|
118
|
-
// // -> Move to the next chain.
|
|
119
|
-
// return resolve(container)
|
|
120
|
-
// })
|
|
121
|
-
// })
|
|
122
|
-
// }
|
|
123
|
-
//
|
|
124
|
-
// // After the conversion we save the Text version to S3.
|
|
125
|
-
// function save_text(container: any) {
|
|
126
|
-
// return new Promise((resolve, reject) => {
|
|
127
|
-
// console.info('save_text')
|
|
128
|
-
//
|
|
129
|
-
// // 1. Set the query
|
|
130
|
-
// const params = {
|
|
131
|
-
// Bucket: container.bucket,
|
|
132
|
-
// Key: `${container.key}.txt`,
|
|
133
|
-
// Body: container.parsed.text,
|
|
134
|
-
// }
|
|
135
|
-
//
|
|
136
|
-
// // -> Execute the query.
|
|
137
|
-
// s3.putObject(params, (error: any, data: any) => {
|
|
138
|
-
// // 1. Check for internal errors.
|
|
139
|
-
// if (error)
|
|
140
|
-
// return reject(error)
|
|
141
|
-
//
|
|
142
|
-
// // -> Move to the next chain.
|
|
143
|
-
// return resolve(container)
|
|
144
|
-
// })
|
|
145
|
-
// })
|
|
146
|
-
// }
|
|
147
|
-
//
|
|
148
|
-
// // Then if we have a HTML version we try to save that.
|
|
149
|
-
// function save_html(container: any) {
|
|
150
|
-
// return new Promise((resolve, reject) => {
|
|
151
|
-
// // <>> When the body of an email have only one version, meaning it
|
|
152
|
-
// // does have only the pure text version and no HTML.
|
|
153
|
-
// // Nodemailer won't generate the HTML for you, it just grabs
|
|
154
|
-
// // what is in the Email body.
|
|
155
|
-
// // So, this value will be false, when there is no HTML content in
|
|
156
|
-
// // the email, and thus we skip this step.
|
|
157
|
-
// if (!container.parsed.html) {
|
|
158
|
-
// console.info('save_html - skipped')
|
|
159
|
-
//
|
|
160
|
-
// // -> Move to the next chain.
|
|
161
|
-
// return resolve(container)
|
|
162
|
-
// }
|
|
163
|
-
//
|
|
164
|
-
// console.info('save_html')
|
|
165
|
-
//
|
|
166
|
-
// // 1. Set the query.
|
|
167
|
-
// const params = {
|
|
168
|
-
// Bucket: container.bucket,
|
|
169
|
-
// Key: `${container.key}.html`,
|
|
170
|
-
// Body: container.parsed.html,
|
|
171
|
-
// }
|
|
172
|
-
//
|
|
173
|
-
// // -> Execute the query.
|
|
174
|
-
// s3.putObject(params, (error: any, data: any) => {
|
|
175
|
-
// // 1. Check for internal errors.
|
|
176
|
-
// if (error)
|
|
177
|
-
// return reject(error)
|
|
178
|
-
//
|
|
179
|
-
// // -> Move to the next chain.
|
|
180
|
-
// return resolve(container)
|
|
181
|
-
// })
|
|
182
|
-
// })
|
|
183
|
-
// }
|
|
184
|
-
//
|
|
185
|
-
// // Last thing to do is to save the attachments if there are any. If the
|
|
186
|
-
// // array is empty the loop will skip itself.
|
|
187
|
-
// function save_attachments(container: any) {
|
|
188
|
-
// return new Promise((resolve, reject) => {
|
|
189
|
-
// console.info('save_attachments')
|
|
190
|
-
//
|
|
191
|
-
// // Start the loop which save all the attachments.
|
|
192
|
-
// loop(1, (error: any) => {
|
|
193
|
-
// // <<> Check if there was an error.
|
|
194
|
-
// if (error) {
|
|
195
|
-
// // -> Stop everything and surface the error.
|
|
196
|
-
// return reject(container)
|
|
197
|
-
// }
|
|
198
|
-
//
|
|
199
|
-
// // -> Move to the next chain.
|
|
200
|
-
// return resolve(container)
|
|
201
|
-
// })
|
|
202
|
-
//
|
|
203
|
-
// // This loop will upload all the individual attachments found in a email
|
|
204
|
-
// function loop(count: any, callback: any) {
|
|
205
|
-
// // 1. Pop the last element in the array if any.
|
|
206
|
-
// const file = container.parsed.attachments.pop()
|
|
207
|
-
//
|
|
208
|
-
// // 2. If there is nothing left then we stop the loop.
|
|
209
|
-
// if (!file)
|
|
210
|
-
// return callback()
|
|
211
|
-
//
|
|
212
|
-
// // 3. Get the file name which also contain the file extension.
|
|
213
|
-
// let file_name = file.filename
|
|
214
|
-
//
|
|
215
|
-
// // 4. An email attachment is not required to have a name, this
|
|
216
|
-
// // mean we need to check if we have a file name and, if not
|
|
217
|
-
// // we create a general name, so all attachments can be saved
|
|
218
|
-
// // and accounted for.
|
|
219
|
-
// if (!file_name) {
|
|
220
|
-
// // 1. Set the generic name with the count name so we give
|
|
221
|
-
// // unique names to each attachment.
|
|
222
|
-
// file_name = `Mail Attachment ${count}`
|
|
223
|
-
//
|
|
224
|
-
// // 2. We only count the times we had to set a generic name.
|
|
225
|
-
// count++
|
|
226
|
-
// }
|
|
227
|
-
//
|
|
228
|
-
// // 5. Split the string to and get the last element of the array
|
|
229
|
-
// // which should be the file extension.
|
|
230
|
-
// const extension = file_name.split('.').pop()
|
|
231
|
-
//
|
|
232
|
-
// // 6. Then we check if the last element is an extension.
|
|
233
|
-
// const is_extension = mime.getType(extension)
|
|
234
|
-
//
|
|
235
|
-
// // 7. If it isn't, we get one from the content-type header.
|
|
236
|
-
// if (!is_extension) {
|
|
237
|
-
// // 1. Make sure we have a content type, since I'm sure
|
|
238
|
-
// // some emails won't have it if they were malformed.
|
|
239
|
-
// if (file.contentType) {
|
|
240
|
-
// // 1. Try to find out the file extension from the
|
|
241
|
-
// // content-type found in the email message.
|
|
242
|
-
// let file_type = mime.getExtension(file.contentType)
|
|
243
|
-
//
|
|
244
|
-
// // 2. If the extension was not found, then we set a
|
|
245
|
-
// // default one which will let the user know
|
|
246
|
-
// // that we don't know the type of the file, while
|
|
247
|
-
// // also setting the type to txt (for convenience)
|
|
248
|
-
// // so it can be open by any editor and inspected.
|
|
249
|
-
// if (!file_type)
|
|
250
|
-
// file_type = 'unknown_extension.txt'
|
|
251
|
-
//
|
|
252
|
-
// // 3. Attach the extension to the file.
|
|
253
|
-
// file_name += `.${file_type}`
|
|
254
|
-
// }
|
|
255
|
-
// }
|
|
256
|
-
//
|
|
257
|
-
// // 8. Then save the buffer of the attachment.
|
|
258
|
-
// const file_body = file.content
|
|
259
|
-
//
|
|
260
|
-
// // 9. Split the S3 Key (path) so we can remove the last element. Since
|
|
261
|
-
// // we don't want the object name, we care only about the path.
|
|
262
|
-
// const tmp = container.key.split('/')
|
|
263
|
-
//
|
|
264
|
-
// // 10. Now remove the last element from the array which is the
|
|
265
|
-
// // file name that contains the raw email, which we don't want.
|
|
266
|
-
// tmp.pop()
|
|
267
|
-
//
|
|
268
|
-
// // 11. After all this, we recombine the array in to a single
|
|
269
|
-
// // string which becomes again the S3 Key minus the file name.
|
|
270
|
-
// const path = tmp.join('/')
|
|
271
|
-
//
|
|
272
|
-
// // 12. This variable is used if there are two files of the same name.
|
|
273
|
-
// let cid = ''
|
|
274
|
-
//
|
|
275
|
-
// // 13. If the CID is set then it means that two files have the same names
|
|
276
|
-
// if (file.cid) {
|
|
277
|
-
// // 1. Add the CID in front of the name of the file so they won't be overwritten
|
|
278
|
-
// cid = `${file.cid} - `
|
|
279
|
-
// }
|
|
280
|
-
//
|
|
281
|
-
// // 14. Create the full key path with the object at the end.
|
|
282
|
-
// const key = `${path}/attachments/${cid}${file_name}`
|
|
283
|
-
//
|
|
284
|
-
// // 15. Set the query.
|
|
285
|
-
// const params = {
|
|
286
|
-
// Bucket: container.bucket,
|
|
287
|
-
// Key: key,
|
|
288
|
-
// Body: file_body,
|
|
289
|
-
// }
|
|
290
|
-
//
|
|
291
|
-
// // -> Execute the query.
|
|
292
|
-
// s3.putObject(params, (error: any, data: any) => {
|
|
293
|
-
// // 1. Check for internal errors.
|
|
294
|
-
// if (error)
|
|
295
|
-
// return callback(error)
|
|
296
|
-
//
|
|
297
|
-
// // -> Move to the next chain.
|
|
298
|
-
// return loop(count, callback)
|
|
299
|
-
// })
|
|
300
|
-
// }
|
|
301
|
-
// })
|
|
302
|
-
// }
|
package/src/server/inbound.ts
DELETED
|
@@ -1,404 +0,0 @@
|
|
|
1
|
-
// import type Stream from 'node:stream'
|
|
2
|
-
// import * as AWS from 'aws-sdk'
|
|
3
|
-
// import moment from 'moment'
|
|
4
|
-
// import { simpleParser as parser } from 'mailparser'
|
|
5
|
-
// import { log } from '@stacksjs/logging'
|
|
6
|
-
//
|
|
7
|
-
// // Initialize S3
|
|
8
|
-
// const s3 = new AWS.S3({
|
|
9
|
-
// apiVersion: '2006-03-01',
|
|
10
|
-
// })
|
|
11
|
-
//
|
|
12
|
-
// // Initialize SES
|
|
13
|
-
// const ses = new AWS.SES({
|
|
14
|
-
// apiVersion: '2010-12-01',
|
|
15
|
-
// })
|
|
16
|
-
//
|
|
17
|
-
// // This variable will hold all the domain available in SES so we don't
|
|
18
|
-
// // have to query SES each time the Lambda runs, we query SES only when
|
|
19
|
-
// // the lambda starts from scratch to circumvent the 1 sec request limit.
|
|
20
|
-
// const domains: string[] = []
|
|
21
|
-
//
|
|
22
|
-
// interface Container {
|
|
23
|
-
// bucket: string
|
|
24
|
-
// unescaped_key: string
|
|
25
|
-
// escaped_key: string
|
|
26
|
-
// domains: string[]
|
|
27
|
-
// folder: string
|
|
28
|
-
// // wip
|
|
29
|
-
// raw_email?: Buffer | Stream | string
|
|
30
|
-
// to?: string
|
|
31
|
-
// from?: string
|
|
32
|
-
// date?: Date | string
|
|
33
|
-
// subject?: string
|
|
34
|
-
// message_id?: string
|
|
35
|
-
// to_domain?: string
|
|
36
|
-
// to_account?: string
|
|
37
|
-
// from_domain?: string
|
|
38
|
-
// from_account?: string
|
|
39
|
-
// path?: string
|
|
40
|
-
// }
|
|
41
|
-
//
|
|
42
|
-
// interface S3Event {
|
|
43
|
-
// Records: {
|
|
44
|
-
// s3: {
|
|
45
|
-
// bucket: {
|
|
46
|
-
// name: string
|
|
47
|
-
// }
|
|
48
|
-
// object: {
|
|
49
|
-
// key: string
|
|
50
|
-
// }
|
|
51
|
-
// }
|
|
52
|
-
// }[]
|
|
53
|
-
// }
|
|
54
|
-
//
|
|
55
|
-
// // This Lambda will filter all the incoming emails based on their From and To field
|
|
56
|
-
// export function handler(event: S3Event): Promise<any> {
|
|
57
|
-
// // if (!event.Records.length)
|
|
58
|
-
// // throw new Error('No records in the event')
|
|
59
|
-
//
|
|
60
|
-
// // This JS object will contain all the data within the chain.
|
|
61
|
-
// const container: Container = {
|
|
62
|
-
// bucket: event.Records[0]?.s3.bucket.name || '',
|
|
63
|
-
// unescaped_key: '',
|
|
64
|
-
// escaped_key: event.Records[0]?.s3.object.key || '',
|
|
65
|
-
// domains,
|
|
66
|
-
// folder: 'Sent',
|
|
67
|
-
// }
|
|
68
|
-
//
|
|
69
|
-
// get_email_domains(container)
|
|
70
|
-
// .then((container) => {
|
|
71
|
-
// return unescape_key(container)
|
|
72
|
-
// }).then((container) => {
|
|
73
|
-
// return load_the_email(container)
|
|
74
|
-
// }).then((container) => {
|
|
75
|
-
// return parse_the_email(container)
|
|
76
|
-
// }).then((container) => {
|
|
77
|
-
// return format_time(container)
|
|
78
|
-
// }).then((container) => {
|
|
79
|
-
// return extract_data(container)
|
|
80
|
-
// }).then((container) => {
|
|
81
|
-
// return where_to_save(container)
|
|
82
|
-
// }).then((container) => {
|
|
83
|
-
// return create_s3_boject_key(container)
|
|
84
|
-
// }).then((container) => {
|
|
85
|
-
// return copy_email_to_inbox(container)
|
|
86
|
-
// }).then((container) => {
|
|
87
|
-
// return copy_email_to_today(container)
|
|
88
|
-
// }).then((container) => {
|
|
89
|
-
// return delete_the_email(container)
|
|
90
|
-
// }).then(() => {
|
|
91
|
-
// return true
|
|
92
|
-
// }).catch((error) => {
|
|
93
|
-
// console.error(error)
|
|
94
|
-
//
|
|
95
|
-
// return false
|
|
96
|
-
// })
|
|
97
|
-
//
|
|
98
|
-
// return Promise.resolve()
|
|
99
|
-
// }
|
|
100
|
-
//
|
|
101
|
-
// // List all the emails added to SES, so we can use them to decided where to
|
|
102
|
-
// // store the emails. If in the Inbox, or sent. This is important when you
|
|
103
|
-
// // upload emails by hand to back them up.
|
|
104
|
-
// //
|
|
105
|
-
// // Without doing this everything goes to the Inbox folder.
|
|
106
|
-
// function get_email_domains(container: Container): Promise<Container> {
|
|
107
|
-
// return new Promise((resolve, reject) => {
|
|
108
|
-
// // If we have already the domains we don't query SES anymore since you
|
|
109
|
-
// // can only query SES once a second. To solve this limitation
|
|
110
|
-
// // we grab the data once, and save it in to memory.
|
|
111
|
-
// if (container.domains.length)
|
|
112
|
-
// return resolve(container)
|
|
113
|
-
//
|
|
114
|
-
// log.info('get_email_domains')
|
|
115
|
-
//
|
|
116
|
-
// const params = {
|
|
117
|
-
// IdentityType: 'Domain',
|
|
118
|
-
// MaxItems: 100,
|
|
119
|
-
// }
|
|
120
|
-
//
|
|
121
|
-
// ses.listIdentities(params, (error, data) => {
|
|
122
|
-
// if (error) {
|
|
123
|
-
// console.error(params)
|
|
124
|
-
// return reject(error)
|
|
125
|
-
// }
|
|
126
|
-
//
|
|
127
|
-
// container.domains = data.Identities
|
|
128
|
-
//
|
|
129
|
-
// return resolve(container)
|
|
130
|
-
// })
|
|
131
|
-
// })
|
|
132
|
-
// }
|
|
133
|
-
//
|
|
134
|
-
// // We need to process the path received by S3 since AWS dose escape the
|
|
135
|
-
// // string in a special way. They escape the string in a HTML style
|
|
136
|
-
// // but for whatever reason they convert spaces in to +ses.
|
|
137
|
-
// function unescape_key(container: Container): Promise<Container> {
|
|
138
|
-
// return new Promise((resolve, reject) => {
|
|
139
|
-
// log.info('unescape_key')
|
|
140
|
-
//
|
|
141
|
-
// // First we convert the + in to spaces
|
|
142
|
-
// const plus_to_space = container.escaped_key.replace(/\+/g, ' ')
|
|
143
|
-
//
|
|
144
|
-
// // And then we unescape the string, other wise we lose real + characters
|
|
145
|
-
// const unescaped_key = decodeURIComponent(plus_to_space)
|
|
146
|
-
//
|
|
147
|
-
// container.unescaped_key = unescaped_key
|
|
148
|
-
//
|
|
149
|
-
// return resolve(container)
|
|
150
|
-
// })
|
|
151
|
-
// }
|
|
152
|
-
//
|
|
153
|
-
// // Load the email that we received from SES.
|
|
154
|
-
// function load_the_email(container: Container): Promise<Container> {
|
|
155
|
-
// return new Promise((resolve, reject) => {
|
|
156
|
-
// log.info('load_the_email')
|
|
157
|
-
//
|
|
158
|
-
// const params = {
|
|
159
|
-
// Bucket: container.bucket,
|
|
160
|
-
// Key: container.unescaped_key,
|
|
161
|
-
// }
|
|
162
|
-
//
|
|
163
|
-
// s3.getObject(params, (error, data) => {
|
|
164
|
-
// if (error) {
|
|
165
|
-
// console.error(params)
|
|
166
|
-
// return reject(error)
|
|
167
|
-
// }
|
|
168
|
-
//
|
|
169
|
-
// // Save the email for the next promise
|
|
170
|
-
// // @ts-expect-error - refactor later
|
|
171
|
-
// container.raw_email = data.Body
|
|
172
|
-
//
|
|
173
|
-
// return resolve(container)
|
|
174
|
-
// })
|
|
175
|
-
// })
|
|
176
|
-
// }
|
|
177
|
-
//
|
|
178
|
-
// // Once the raw email is loaded we parse it with one goal in mind, get
|
|
179
|
-
// // the date the of the email. This way we don't rely on the SES date, but
|
|
180
|
-
// // on the real date the email was created.
|
|
181
|
-
// //
|
|
182
|
-
// // This way we can even load in to the system old emails as long as they
|
|
183
|
-
// // are in the standard raw email format, and not some proprietary solution.
|
|
184
|
-
// //
|
|
185
|
-
// // That why will be organized with the time the emails were created, and not
|
|
186
|
-
// // received in to the system.
|
|
187
|
-
// function parse_the_email(container: Container): Promise<Container> {
|
|
188
|
-
// return new Promise((resolve, reject) => {
|
|
189
|
-
// log.info('parse_the_email')
|
|
190
|
-
//
|
|
191
|
-
// if (!container.raw_email)
|
|
192
|
-
// return reject(Error('No raw email to parse'))
|
|
193
|
-
//
|
|
194
|
-
// // Parse the email and extract all that is necessary
|
|
195
|
-
// parser(container.raw_email, (error, data) => {
|
|
196
|
-
// // Check for internal errors
|
|
197
|
-
// if (error) {
|
|
198
|
-
// console.error(data)
|
|
199
|
-
// return reject(error)
|
|
200
|
-
// }
|
|
201
|
-
//
|
|
202
|
-
// // Save the parsed email for the next promise.
|
|
203
|
-
// container.date = data.date
|
|
204
|
-
// container.from = data.from?.value[0]?.address
|
|
205
|
-
// // container.to = data.to?.value[0]?.address
|
|
206
|
-
// container.to = data.from?.value[0]?.address
|
|
207
|
-
// container.subject = data.subject || 'No Subject'
|
|
208
|
-
// container.message_id = data.messageId
|
|
209
|
-
//
|
|
210
|
-
// return resolve(container)
|
|
211
|
-
// })
|
|
212
|
-
// })
|
|
213
|
-
// }
|
|
214
|
-
//
|
|
215
|
-
// // We format the date and time to make sure that when the folder is saved
|
|
216
|
-
// // in S3, the object will sort one after the other which makes it much
|
|
217
|
-
// // easier to see the latest emails.
|
|
218
|
-
// function format_time(container: Container): Promise<Container> {
|
|
219
|
-
// return new Promise((resolve, reject) => {
|
|
220
|
-
// log.info('format_time')
|
|
221
|
-
//
|
|
222
|
-
// // Format the date found in the email message itself
|
|
223
|
-
// container.date = moment(container.date).format('YYYY-MM-DD HH:mm:ss Z')
|
|
224
|
-
//
|
|
225
|
-
// return resolve(container)
|
|
226
|
-
// })
|
|
227
|
-
// }
|
|
228
|
-
//
|
|
229
|
-
// // Extract all the data necessary to organize the incoming emails.
|
|
230
|
-
// function extract_data(container: Container): Promise<Container> {
|
|
231
|
-
// return new Promise((resolve, reject) => {
|
|
232
|
-
// log.info('extract_data')
|
|
233
|
-
//
|
|
234
|
-
// if (!container.to)
|
|
235
|
-
// return reject(Error('No To field in the email'))
|
|
236
|
-
//
|
|
237
|
-
// // Since the email string can come in a form of:
|
|
238
|
-
// // Name Last <name@example.com>
|
|
239
|
-
// // We have to extract just the email address, and discard the rest
|
|
240
|
-
// let tmp_to
|
|
241
|
-
// if (container?.to) {
|
|
242
|
-
// const matchResult = container.to.match(/(?:[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|\\[\x01-\x09\x0B\x0C\x0E-\x7F])*")@(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-zA-Z0-9-]*[a-zA-Z0-9]:(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21-\x5A\x53-\x7F]|\\[\x01-\x09\x0B\x0C\x0E-\x7F])+)\])/gm);
|
|
243
|
-
// if (matchResult) {
|
|
244
|
-
// tmp_to = matchResult[0].split('@');
|
|
245
|
-
// // Continue with your logic using tmp_to
|
|
246
|
-
// }
|
|
247
|
-
// }
|
|
248
|
-
//
|
|
249
|
-
// let tmp_from
|
|
250
|
-
// if (container?.from) {
|
|
251
|
-
// const matchResult = container.from.match(/(?:[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|\\[\x01-\x09\x0B\x0C\x0E-\x7F])*")@(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-zA-Z0-9-]*[a-zA-Z0-9]:(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21-\x5A\x53-\x7F]|\\[\x01-\x09\x0B\x0C\x0E-\x7F])+)\])/gm)
|
|
252
|
-
// if (matchResult) {
|
|
253
|
-
// tmp_from = matchResult[0].split('@');
|
|
254
|
-
// // Continue with your logic using tmp_to
|
|
255
|
-
// }
|
|
256
|
-
// }
|
|
257
|
-
//
|
|
258
|
-
// if (!tmp_to || !tmp_from)
|
|
259
|
-
// return reject(Error('No To or From field in the email'))
|
|
260
|
-
//
|
|
261
|
-
// // Get the domain name of the receiving end, so we can group
|
|
262
|
-
// // emails by all the domain that were added to SES.
|
|
263
|
-
// container.to_domain = tmp_to[1]
|
|
264
|
-
//
|
|
265
|
-
// // Based on the email name, we replace all the + characters, that
|
|
266
|
-
// // can be used to organize ones on-line accounts in to /, this way
|
|
267
|
-
// // we can build a S3 patch which will automatically organize
|
|
268
|
-
// // all the email in structured folder.
|
|
269
|
-
// container.to_account = tmp_to[0]?.replace(/\+/g, '/')
|
|
270
|
-
//
|
|
271
|
-
// // Get the domain name of the email which in our case will become the company name
|
|
272
|
-
// container.from_domain = tmp_from[1]
|
|
273
|
-
//
|
|
274
|
-
// // Get the name of who sent us the email
|
|
275
|
-
// container.from_account = tmp_from[0]
|
|
276
|
-
//
|
|
277
|
-
// // Set all the strings to lower case, because some times different
|
|
278
|
-
// // on-line services will have your email in all caps. For example
|
|
279
|
-
// // Priceline will do this.
|
|
280
|
-
// //
|
|
281
|
-
// // Since in the next step we compare the domain that is in SES
|
|
282
|
-
// // with the data from the email, wee need to have it all in the
|
|
283
|
-
// // same format for the if() statement to work correctly and
|
|
284
|
-
// // generate the right path where to save the email.
|
|
285
|
-
// container.to_domain = container.to_domain?.toLowerCase()
|
|
286
|
-
// container.to_account = container.to_account?.toLowerCase()
|
|
287
|
-
// container.from_domain = container.from_domain?.toLowerCase()
|
|
288
|
-
// container.from_account = container.from_account?.toLowerCase()
|
|
289
|
-
//
|
|
290
|
-
// // S3 objects have a limit of how they they can be named
|
|
291
|
-
// // so we remove everything but...
|
|
292
|
-
// container.subject = container.subject?.replace(/[^a-zA-Z0-9 &@:,$=+?;]/g, '_')
|
|
293
|
-
//
|
|
294
|
-
// return resolve(container)
|
|
295
|
-
// })
|
|
296
|
-
// }
|
|
297
|
-
//
|
|
298
|
-
// // Once we have the domains from SES, and we know the value of the To
|
|
299
|
-
// // field, we can decide where the emails should be saved.
|
|
300
|
-
// function where_to_save(container: Container): Promise<Container> {
|
|
301
|
-
// return new Promise((resolve, reject) => {
|
|
302
|
-
// log.info('where_to_save')
|
|
303
|
-
//
|
|
304
|
-
// // Since by default we assume that the email should be saved
|
|
305
|
-
// // in the Sent folder, we need to loop over the SES domains
|
|
306
|
-
// // and check if there is a match with the To: domain found in
|
|
307
|
-
// // the email.
|
|
308
|
-
// container.domains.forEach((domain) => {
|
|
309
|
-
// if (domain === container.to_domain)
|
|
310
|
-
// container.folder = 'Inbox'
|
|
311
|
-
// })
|
|
312
|
-
//
|
|
313
|
-
// return resolve(container)
|
|
314
|
-
// })
|
|
315
|
-
// }
|
|
316
|
-
//
|
|
317
|
-
// // Create Key path for the S3 object to be saved to.
|
|
318
|
-
// function create_s3_boject_key(container: Container): Promise<Container> {
|
|
319
|
-
// return new Promise((resolve, reject) => {
|
|
320
|
-
// log.info('create_s3_boject_key')
|
|
321
|
-
//
|
|
322
|
-
// // create the path where the email needs to be moved for house keeping
|
|
323
|
-
// container.path = `${container.to_domain}/${container.to_account}/${container.from_domain}/${container.from_account}/${container.date} - ${container.subject}/` + 'email.eml'
|
|
324
|
-
//
|
|
325
|
-
// return resolve(container)
|
|
326
|
-
// })
|
|
327
|
-
// }
|
|
328
|
-
//
|
|
329
|
-
// // Copy the email to a new location - we don't put the email that we
|
|
330
|
-
// // already have in memory since the system requires a COPY action and not
|
|
331
|
-
// // a PUT action.
|
|
332
|
-
// // TODO: refactor below
|
|
333
|
-
// // WARNING: We are using the escaped_key value, because there is a
|
|
334
|
-
// // known bug in the AWS JS SDK which won't unescape the
|
|
335
|
-
// // string, so you have to do it - AWS is aware of this issue.
|
|
336
|
-
// function copy_email_to_inbox(container: Container): Promise<Container> {
|
|
337
|
-
// return new Promise((resolve, reject) => {
|
|
338
|
-
// log.info('copy_email_to_inbox')
|
|
339
|
-
//
|
|
340
|
-
// const params = {
|
|
341
|
-
// Bucket: container.bucket,
|
|
342
|
-
// CopySource: `${container.bucket}/${container.escaped_key}`,
|
|
343
|
-
// Key: `${container.folder}/${container.path}`,
|
|
344
|
-
// }
|
|
345
|
-
//
|
|
346
|
-
// s3.copyObject(params, (error, data) => {
|
|
347
|
-
// if (error) {
|
|
348
|
-
// console.error(params)
|
|
349
|
-
// return reject(error)
|
|
350
|
-
// }
|
|
351
|
-
//
|
|
352
|
-
// return resolve(container)
|
|
353
|
-
// })
|
|
354
|
-
// })
|
|
355
|
-
// }
|
|
356
|
-
//
|
|
357
|
-
// // Similarly to the previous promise, but in this case we COPY the email to
|
|
358
|
-
// // the Today folder to showcase all the latest emails that were received
|
|
359
|
-
// // within 24h.
|
|
360
|
-
// //
|
|
361
|
-
// // There is a Lifecycle rule set on the bucket to delete any object inside
|
|
362
|
-
// // the Today folder if it is older then 24h.
|
|
363
|
-
// //
|
|
364
|
-
// // This way it is easier to see all the latest emails.
|
|
365
|
-
// function copy_email_to_today(container: Container): Promise<Container> {
|
|
366
|
-
// return new Promise((resolve, reject) => {
|
|
367
|
-
// log.info('copy_email_to_today')
|
|
368
|
-
//
|
|
369
|
-
// const params = {
|
|
370
|
-
// Bucket: container.bucket,
|
|
371
|
-
// CopySource: `${container.bucket}/${container.escaped_key}`,
|
|
372
|
-
// Key: `today/${container.path}`,
|
|
373
|
-
// }
|
|
374
|
-
//
|
|
375
|
-
// s3.copyObject(params, (error, data) => {
|
|
376
|
-
// if (error) {
|
|
377
|
-
// console.error(params)
|
|
378
|
-
// return reject(error)
|
|
379
|
-
// }
|
|
380
|
-
//
|
|
381
|
-
// return resolve(container)
|
|
382
|
-
// })
|
|
383
|
-
// })
|
|
384
|
-
// }
|
|
385
|
-
//
|
|
386
|
-
// function delete_the_email(container: Container): Promise<Container> {
|
|
387
|
-
// return new Promise((resolve, reject) => {
|
|
388
|
-
// log.info('delete_the_email')
|
|
389
|
-
//
|
|
390
|
-
// const params = {
|
|
391
|
-
// Bucket: container.bucket,
|
|
392
|
-
// Key: container.unescaped_key,
|
|
393
|
-
// }
|
|
394
|
-
//
|
|
395
|
-
// s3.deleteObject(params, (error, data) => {
|
|
396
|
-
// if (error) {
|
|
397
|
-
// console.error(params)
|
|
398
|
-
// return reject(error)
|
|
399
|
-
// }
|
|
400
|
-
//
|
|
401
|
-
// return resolve(container)
|
|
402
|
-
// })
|
|
403
|
-
// })
|
|
404
|
-
// }
|
package/src/server/outbound.ts
DELETED
|
@@ -1,296 +0,0 @@
|
|
|
1
|
-
// /* eslint-disable eslint-comments/no-unlimited-disable */
|
|
2
|
-
// /* eslint-disable */
|
|
3
|
-
//
|
|
4
|
-
// const AWS = require('aws-sdk')
|
|
5
|
-
// const moment = require('moment')
|
|
6
|
-
// const uuidv1 = require('uuid/v1')
|
|
7
|
-
// const MailComposer = require('nodemailer/lib/mail-composer')
|
|
8
|
-
//
|
|
9
|
-
// // Initialize S3
|
|
10
|
-
// const s3 = new AWS.S3({
|
|
11
|
-
// apiVersion: '2006-03-01',
|
|
12
|
-
// })
|
|
13
|
-
//
|
|
14
|
-
// // Initialize SES
|
|
15
|
-
// const ses = new AWS.SES({
|
|
16
|
-
// apiVersion: '2010-12-01',
|
|
17
|
-
// })
|
|
18
|
-
//
|
|
19
|
-
// // This lambda will read outgoing emails formatted in JSON, convert them
|
|
20
|
-
// // in to a raw email, send them using SES, and store them in S3.
|
|
21
|
-
// exports.handler = (event: any) => {
|
|
22
|
-
// // This JS object will contain all the data within the chain
|
|
23
|
-
// const container = {
|
|
24
|
-
// bucket: event.Records[0].s3.bucket.name,
|
|
25
|
-
// key: event.Records[0].s3.object.key,
|
|
26
|
-
// email: {
|
|
27
|
-
// json: {},
|
|
28
|
-
// raw: '',
|
|
29
|
-
// },
|
|
30
|
-
// uuid: uuidv1(),
|
|
31
|
-
// }
|
|
32
|
-
//
|
|
33
|
-
// // -> Start the chain.
|
|
34
|
-
// load_the_email(container)
|
|
35
|
-
// .then((container) => {
|
|
36
|
-
// return extract_data(container)
|
|
37
|
-
// }).then((container) => {
|
|
38
|
-
// return generate_the_raw_email(container)
|
|
39
|
-
// }).then((container) => {
|
|
40
|
-
// return send_email(container)
|
|
41
|
-
// }).then((container) => {
|
|
42
|
-
// return save_raw_email(container)
|
|
43
|
-
// }).then((container) => {
|
|
44
|
-
// return copy_raw_email(container)
|
|
45
|
-
// }).then((container) => {
|
|
46
|
-
// return delete_json_email(container)
|
|
47
|
-
// }).then((container) => {
|
|
48
|
-
// return delete_raw_email(container)
|
|
49
|
-
// }).then((container) => {
|
|
50
|
-
// return true
|
|
51
|
-
// }).catch((error) => {
|
|
52
|
-
// console.error(error)
|
|
53
|
-
//
|
|
54
|
-
// return false
|
|
55
|
-
// })
|
|
56
|
-
// }
|
|
57
|
-
//
|
|
58
|
-
// // Load the JSON email that triggered this Lambda.
|
|
59
|
-
// function load_the_email(container: any) {
|
|
60
|
-
// return new Promise((resolve, reject) => {
|
|
61
|
-
// console.info('load_the_email')
|
|
62
|
-
//
|
|
63
|
-
// // 1. Set the query.
|
|
64
|
-
// const params = {
|
|
65
|
-
// Bucket: container.bucket,
|
|
66
|
-
// Key: container.key,
|
|
67
|
-
// }
|
|
68
|
-
//
|
|
69
|
-
// // -> Execute the query.
|
|
70
|
-
// s3.getObject(params, (error: any, data: any) => {
|
|
71
|
-
// // 1. Check for internal errors.
|
|
72
|
-
// if (error)
|
|
73
|
-
// return reject(error)
|
|
74
|
-
//
|
|
75
|
-
// // 2. Save the email for the next promise.
|
|
76
|
-
// container.email.json = JSON.parse(data.Body)
|
|
77
|
-
//
|
|
78
|
-
// // -> Move to the next chain.
|
|
79
|
-
// return resolve(container)
|
|
80
|
-
// })
|
|
81
|
-
// })
|
|
82
|
-
// }
|
|
83
|
-
//
|
|
84
|
-
// // Extract all the data necessary to organize the incoming emails.
|
|
85
|
-
// function extract_data(container: any) {
|
|
86
|
-
// return new Promise((resolve, reject) => {
|
|
87
|
-
// console.info('extract_data')
|
|
88
|
-
//
|
|
89
|
-
// // 1. Extract all the information
|
|
90
|
-
// const tmp_to = container.email.json
|
|
91
|
-
// .to
|
|
92
|
-
// .match(/[a-z0-9-+]{1,30}@[a-z0-9-]{1,65}.[a-z]{1,}/gm)[0]
|
|
93
|
-
// .split('@')
|
|
94
|
-
//
|
|
95
|
-
// const tmp_from = container.email.json
|
|
96
|
-
// .from
|
|
97
|
-
// .match(/[a-z0-9-+]{1,30}@[a-z0-9-]{1,65}.[a-z]{1,}/gm)[0]
|
|
98
|
-
// .split('@')
|
|
99
|
-
//
|
|
100
|
-
// // 2. Get the domain name of the receiving end, so we can group
|
|
101
|
-
// // emails by all the domain that were added to SES.
|
|
102
|
-
// const to_domain = tmp_to[1]
|
|
103
|
-
//
|
|
104
|
-
// // 3. Based on the email name, we replace all the + characters, that
|
|
105
|
-
// // can be used to organize ones on-line accounts in to /, this way
|
|
106
|
-
// // we can build a S3 patch which will automatically organize
|
|
107
|
-
// // all the email in structured folder.
|
|
108
|
-
// const to_account = tmp_to[0].replace(/\+/g, '/')
|
|
109
|
-
//
|
|
110
|
-
// // 4. Get the domain name of the email which in our case will
|
|
111
|
-
// // become the company name.
|
|
112
|
-
// const from_domain = tmp_from[1]
|
|
113
|
-
//
|
|
114
|
-
// // 5. Get the name of who sent us the email.
|
|
115
|
-
// const from_account = tmp_from[0]
|
|
116
|
-
//
|
|
117
|
-
// // 6. Create a human readable time stamp that matches the format
|
|
118
|
-
// // S3 Provides in its Event.
|
|
119
|
-
// const date = moment().format('ddd, DD MMM YYYY HH:MM:SS ZZ')
|
|
120
|
-
//
|
|
121
|
-
// // 7. Create the path where the email needs to be moved
|
|
122
|
-
// // so it is properly organized.
|
|
123
|
-
// const path = `sent/${
|
|
124
|
-
// to_domain
|
|
125
|
-
// }/${
|
|
126
|
-
// to_account
|
|
127
|
-
// }/${
|
|
128
|
-
// from_domain
|
|
129
|
-
// }/${
|
|
130
|
-
// from_account
|
|
131
|
-
// }/${
|
|
132
|
-
// date
|
|
133
|
-
// } - ${
|
|
134
|
-
// container.email.json.subject
|
|
135
|
-
// }/`
|
|
136
|
-
// + 'email'
|
|
137
|
-
//
|
|
138
|
-
// // 8. Save the path for the next promise.
|
|
139
|
-
// container.path = path
|
|
140
|
-
//
|
|
141
|
-
// // -> Move to the next chain.
|
|
142
|
-
// return resolve(container)
|
|
143
|
-
// })
|
|
144
|
-
// }
|
|
145
|
-
//
|
|
146
|
-
// // From the JSON file that we loaded in to memory we generate a RAW version that
|
|
147
|
-
// // can be used by SES, and later on stored and converted in multiple formats
|
|
148
|
-
// function generate_the_raw_email(container: any) {
|
|
149
|
-
// return new Promise((resolve, reject) => {
|
|
150
|
-
// console.info('generate_the_raw_email')
|
|
151
|
-
//
|
|
152
|
-
// // 1. Crete the email based on the message object which holds all the
|
|
153
|
-
// // necessary information.
|
|
154
|
-
// const mail = new MailComposer(container.email.json)
|
|
155
|
-
//
|
|
156
|
-
// // 2. Take the email and compile it down to its text form for storage.
|
|
157
|
-
// mail.compile().build((error: any, raw_message: any) => {
|
|
158
|
-
// // 1. Check if there was an error
|
|
159
|
-
// if (error)
|
|
160
|
-
// return reject(error)
|
|
161
|
-
//
|
|
162
|
-
// // 2. Save the raw email so we can save it as is in to S3.
|
|
163
|
-
// container.email.raw = raw_message
|
|
164
|
-
//
|
|
165
|
-
// // -> Move to the next promise
|
|
166
|
-
// return resolve(container)
|
|
167
|
-
// })
|
|
168
|
-
// })
|
|
169
|
-
// }
|
|
170
|
-
//
|
|
171
|
-
// // Use the newly generated RAW email and send it using SES.
|
|
172
|
-
// function send_email(container: any) {
|
|
173
|
-
// return new Promise((resolve, reject) => {
|
|
174
|
-
// console.info('send_email')
|
|
175
|
-
//
|
|
176
|
-
// // 1. Create the message
|
|
177
|
-
// const params = {
|
|
178
|
-
// RawMessage: {
|
|
179
|
-
// Data: container.email.raw,
|
|
180
|
-
// },
|
|
181
|
-
// }
|
|
182
|
-
//
|
|
183
|
-
// // -> Send the email out
|
|
184
|
-
// ses.sendRawEmail(params, (error: any, data: any) => {
|
|
185
|
-
// // 1. Check if there was an error
|
|
186
|
-
// if (error)
|
|
187
|
-
// return reject(error)
|
|
188
|
-
//
|
|
189
|
-
// // -> Move to the next chain
|
|
190
|
-
// return resolve(container)
|
|
191
|
-
// })
|
|
192
|
-
// })
|
|
193
|
-
// }
|
|
194
|
-
//
|
|
195
|
-
// // We now save the RAW email to S3 in a temporary folder. We do this because
|
|
196
|
-
// // we don't want to create a PUT action in the Sent folder. The reason is
|
|
197
|
-
// // that if we were to put the object directly to the destination, it would
|
|
198
|
-
// // create a PUT action which would trigger the Convert Lambda. And this is
|
|
199
|
-
// // fine. But then then Convert Lambda would save the object with a PUT action
|
|
200
|
-
// // which would trigger the Convert Lambda once more, and until infinity.
|
|
201
|
-
// // To prevent this loop, we make a temporary PUT event with no triggers,
|
|
202
|
-
// // and then we copy the object to the final destination, which triggers
|
|
203
|
-
// // the Convert action, and once the convert action makes a PUT nothing else
|
|
204
|
-
// // happens.
|
|
205
|
-
// function save_raw_email(container: any) {
|
|
206
|
-
// return new Promise((resolve, reject) => {
|
|
207
|
-
// console.info('save_raw_email')
|
|
208
|
-
//
|
|
209
|
-
// // 1. Set the query.
|
|
210
|
-
// const params = {
|
|
211
|
-
// Bucket: container.bucket,
|
|
212
|
-
// Key: `tmp/email_out/raw/${container.uuid}`,
|
|
213
|
-
// Body: container.email.raw,
|
|
214
|
-
// }
|
|
215
|
-
//
|
|
216
|
-
// // -> Execute the query.
|
|
217
|
-
// s3.putObject(params, (error: any, data: any) => {
|
|
218
|
-
// // 1. Check for internal errors.
|
|
219
|
-
// if (error)
|
|
220
|
-
// return reject(error)
|
|
221
|
-
//
|
|
222
|
-
// // -> Move to the next chain.
|
|
223
|
-
// return resolve(container)
|
|
224
|
-
// })
|
|
225
|
-
// })
|
|
226
|
-
// }
|
|
227
|
-
//
|
|
228
|
-
// // Now that we have our RAW email saved we do a copy event so it will trigger the Conversion Lambda
|
|
229
|
-
// function copy_raw_email(container: any) {
|
|
230
|
-
// return new Promise((resolve, reject) => {
|
|
231
|
-
// console.info('copy_raw_email')
|
|
232
|
-
//
|
|
233
|
-
// // 1. Set the query.
|
|
234
|
-
// const params = {
|
|
235
|
-
// Bucket: container.bucket,
|
|
236
|
-
// CopySource: `${container.bucket}/tmp/email_out/raw/${container.uuid}`,
|
|
237
|
-
// Key: container.path,
|
|
238
|
-
// }
|
|
239
|
-
//
|
|
240
|
-
// // -> Execute the query.
|
|
241
|
-
// s3.copyObject(params, (error: any, data: any) => {
|
|
242
|
-
// // 1. Check for internal errors.
|
|
243
|
-
// if (error)
|
|
244
|
-
// return reject(error)
|
|
245
|
-
//
|
|
246
|
-
// // -> Move to the next chain.
|
|
247
|
-
// return resolve(container)
|
|
248
|
-
// })
|
|
249
|
-
// })
|
|
250
|
-
// }
|
|
251
|
-
//
|
|
252
|
-
// // And before we finish we clean up the environment by deleting the JSON email,
|
|
253
|
-
// function delete_json_email(container: any) {
|
|
254
|
-
// return new Promise((resolve, reject) => {
|
|
255
|
-
// console.info('delete_json_email')
|
|
256
|
-
//
|
|
257
|
-
// // 1. Set the query.
|
|
258
|
-
// const params = {
|
|
259
|
-
// Bucket: container.bucket,
|
|
260
|
-
// Key: container.key,
|
|
261
|
-
// }
|
|
262
|
-
//
|
|
263
|
-
// // -> Execute the query.
|
|
264
|
-
// s3.deleteObject(params, (error: any, data: any) => {
|
|
265
|
-
// // 1. Check for internal errors.
|
|
266
|
-
// if (error)
|
|
267
|
-
// return reject(error)
|
|
268
|
-
//
|
|
269
|
-
// // -> Move to the next chain.
|
|
270
|
-
// return resolve(container)
|
|
271
|
-
// })
|
|
272
|
-
// })
|
|
273
|
-
// }
|
|
274
|
-
//
|
|
275
|
-
// // And the temporary RAW email.
|
|
276
|
-
// function delete_raw_email(container: any) {
|
|
277
|
-
// return new Promise((resolve, reject) => {
|
|
278
|
-
// console.info('delete_raw_email')
|
|
279
|
-
//
|
|
280
|
-
// // 1. Set the query.
|
|
281
|
-
// const params = {
|
|
282
|
-
// Bucket: container.bucket,
|
|
283
|
-
// Key: `tmp/email_out/raw/${container.uuid}`,
|
|
284
|
-
// }
|
|
285
|
-
//
|
|
286
|
-
// // -> Execute the query.
|
|
287
|
-
// s3.deleteObject(params, (error: any, data: any) => {
|
|
288
|
-
// // 1. Check for internal errors.
|
|
289
|
-
// if (error)
|
|
290
|
-
// return reject(error)
|
|
291
|
-
//
|
|
292
|
-
// // -> Move to the next chain.
|
|
293
|
-
// return resolve(container)
|
|
294
|
-
// })
|
|
295
|
-
// })
|
|
296
|
-
// }
|
package/src/template.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type { I18n } from 'vue-email'
|
|
2
|
-
import process from 'node:process'
|
|
3
|
-
import { resourcesPath } from '@stacksjs/path'
|
|
4
|
-
import { config } from '@vue-email/compiler'
|
|
5
|
-
|
|
6
|
-
export interface RenderOptions {
|
|
7
|
-
props?: Record<string, unknown>
|
|
8
|
-
i18n?: I18n
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export async function template(path: string, options?: RenderOptions): Promise<string> {
|
|
12
|
-
const email = config(resourcesPath('emails'), {
|
|
13
|
-
verbose: !!process.env.DEBUG,
|
|
14
|
-
// options: {
|
|
15
|
-
// baseUrl: 'https://APP_URL/',
|
|
16
|
-
// },
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
return await email.render(path, options)
|
|
20
|
-
}
|
package/src/types.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
export interface Message {
|
|
2
|
-
name: string
|
|
3
|
-
subject: string
|
|
4
|
-
to: string
|
|
5
|
-
from?: {
|
|
6
|
-
name: string
|
|
7
|
-
address: string
|
|
8
|
-
}
|
|
9
|
-
template: string
|
|
10
|
-
handle?: () => Promise<{ message: string }> // optional because it may be a simple template
|
|
11
|
-
onError?: (error: Error) => Promise<{ message: string }>
|
|
12
|
-
onSuccess?: () => void
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export interface SendEmailParams {
|
|
16
|
-
Source: string
|
|
17
|
-
Destination: {
|
|
18
|
-
ToAddresses: string[]
|
|
19
|
-
}
|
|
20
|
-
Message: {
|
|
21
|
-
Body: {
|
|
22
|
-
Html: {
|
|
23
|
-
Charset: 'UTF-8'
|
|
24
|
-
Data: string
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
Subject: {
|
|
28
|
-
Charset: 'UTF-8'
|
|
29
|
-
Data: string
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export interface EmailParams {
|
|
35
|
-
to: string
|
|
36
|
-
from: string
|
|
37
|
-
subject: string
|
|
38
|
-
html: string
|
|
39
|
-
}
|