@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.
@@ -1,406 +1,406 @@
1
- /* eslint-disable eslint-comments/no-unlimited-disable */
2
- /* eslint-disable */
3
- import type Stream from 'node:stream'
4
- import * as AWS from 'aws-sdk'
5
- import moment from 'moment'
6
- import { simpleParser as parser } from 'mailparser'
7
- import { log } from '@stacksjs/logging'
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 variable will hold all the domain available in SES so we don't
20
- // have to query SES each time the Lambda runs, we query SES only when
21
- // the lambda starts from scratch to circumvent the 1 sec request limit.
22
- const domains: string[] = []
23
-
24
- interface Container {
25
- bucket: string
26
- unescaped_key: string
27
- escaped_key: string
28
- domains: string[]
29
- folder: string
30
- // wip
31
- raw_email?: Buffer | Stream | string
32
- to?: string
33
- from?: string
34
- date?: Date | string
35
- subject?: string
36
- message_id?: string
37
- to_domain?: string
38
- to_account?: string
39
- from_domain?: string
40
- from_account?: string
41
- path?: string
42
- }
43
-
44
- interface S3Event {
45
- Records: {
46
- s3: {
47
- bucket: {
48
- name: string
49
- }
50
- object: {
51
- key: string
52
- }
53
- }
54
- }[]
55
- }
56
-
57
- // This Lambda will filter all the incoming emails based on their From and To field
58
- export function handler(event: S3Event): Promise<any> {
59
- // if (!event.Records.length)
60
- // throw new Error('No records in the event')
61
-
62
- // This JS object will contain all the data within the chain.
63
- const container: Container = {
64
- bucket: event.Records[0]?.s3.bucket.name || '',
65
- unescaped_key: '',
66
- escaped_key: event.Records[0]?.s3.object.key || '',
67
- domains,
68
- folder: 'Sent',
69
- }
70
-
71
- get_email_domains(container)
72
- .then((container) => {
73
- return unescape_key(container)
74
- }).then((container) => {
75
- return load_the_email(container)
76
- }).then((container) => {
77
- return parse_the_email(container)
78
- }).then((container) => {
79
- return format_time(container)
80
- }).then((container) => {
81
- return extract_data(container)
82
- }).then((container) => {
83
- return where_to_save(container)
84
- }).then((container) => {
85
- return create_s3_boject_key(container)
86
- }).then((container) => {
87
- return copy_email_to_inbox(container)
88
- }).then((container) => {
89
- return copy_email_to_today(container)
90
- }).then((container) => {
91
- return delete_the_email(container)
92
- }).then(() => {
93
- return true
94
- }).catch((error) => {
95
- console.error(error)
96
-
97
- return false
98
- })
99
-
100
- return Promise.resolve()
101
- }
102
-
103
- // List all the emails added to SES, so we can use them to decided where to
104
- // store the emails. If in the Inbox, or sent. This is important when you
105
- // upload emails by hand to back them up.
106
- //
107
- // Without doing this everything goes to the Inbox folder.
108
- function get_email_domains(container: Container): Promise<Container> {
109
- return new Promise((resolve, reject) => {
110
- // If we have already the domains we don't query SES anymore since you
111
- // can only query SES once a second. To solve this limitation
112
- // we grab the data once, and save it in to memory.
113
- if (container.domains.length)
114
- return resolve(container)
115
-
116
- log.info('get_email_domains')
117
-
118
- const params = {
119
- IdentityType: 'Domain',
120
- MaxItems: 100,
121
- }
122
-
123
- ses.listIdentities(params, (error, data) => {
124
- if (error) {
125
- console.error(params)
126
- return reject(error)
127
- }
128
-
129
- container.domains = data.Identities
130
-
131
- return resolve(container)
132
- })
133
- })
134
- }
135
-
136
- // We need to process the path received by S3 since AWS dose escape the
137
- // string in a special way. They escape the string in a HTML style
138
- // but for whatever reason they convert spaces in to +ses.
139
- function unescape_key(container: Container): Promise<Container> {
140
- return new Promise((resolve, reject) => {
141
- log.info('unescape_key')
142
-
143
- // First we convert the + in to spaces
144
- const plus_to_space = container.escaped_key.replace(/\+/g, ' ')
145
-
146
- // And then we unescape the string, other wise we lose real + characters
147
- const unescaped_key = decodeURIComponent(plus_to_space)
148
-
149
- container.unescaped_key = unescaped_key
150
-
151
- return resolve(container)
152
- })
153
- }
154
-
155
- // Load the email that we received from SES.
156
- function load_the_email(container: Container): Promise<Container> {
157
- return new Promise((resolve, reject) => {
158
- log.info('load_the_email')
159
-
160
- const params = {
161
- Bucket: container.bucket,
162
- Key: container.unescaped_key,
163
- }
164
-
165
- s3.getObject(params, (error, data) => {
166
- if (error) {
167
- console.error(params)
168
- return reject(error)
169
- }
170
-
171
- // Save the email for the next promise
172
- // @ts-expect-error - refactor later
173
- container.raw_email = data.Body
174
-
175
- return resolve(container)
176
- })
177
- })
178
- }
179
-
180
- // Once the raw email is loaded we parse it with one goal in mind, get
181
- // the date the of the email. This way we don't rely on the SES date, but
182
- // on the real date the email was created.
183
- //
184
- // This way we can even load in to the system old emails as long as they
185
- // are in the standard raw email format, and not some proprietary solution.
186
- //
187
- // That why will be organized with the time the emails were created, and not
188
- // received in to the system.
189
- function parse_the_email(container: Container): Promise<Container> {
190
- return new Promise((resolve, reject) => {
191
- log.info('parse_the_email')
192
-
193
- if (!container.raw_email)
194
- return reject(Error('No raw email to parse'))
195
-
196
- // Parse the email and extract all that is necessary
197
- parser(container.raw_email, (error, data) => {
198
- // Check for internal errors
199
- if (error) {
200
- console.error(data)
201
- return reject(error)
202
- }
203
-
204
- // Save the parsed email for the next promise.
205
- container.date = data.date
206
- container.from = data.from?.value[0]?.address
207
- // container.to = data.to?.value[0]?.address
208
- container.to = data.from?.value[0]?.address
209
- container.subject = data.subject || 'No Subject'
210
- container.message_id = data.messageId
211
-
212
- return resolve(container)
213
- })
214
- })
215
- }
216
-
217
- // We format the date and time to make sure that when the folder is saved
218
- // in S3, the object will sort one after the other which makes it much
219
- // easier to see the latest emails.
220
- function format_time(container: Container): Promise<Container> {
221
- return new Promise((resolve, reject) => {
222
- log.info('format_time')
223
-
224
- // Format the date found in the email message itself
225
- container.date = moment(container.date).format('YYYY-MM-DD HH:mm:ss Z')
226
-
227
- return resolve(container)
228
- })
229
- }
230
-
231
- // Extract all the data necessary to organize the incoming emails.
232
- function extract_data(container: Container): Promise<Container> {
233
- return new Promise((resolve, reject) => {
234
- log.info('extract_data')
235
-
236
- if (!container.to)
237
- return reject(Error('No To field in the email'))
238
-
239
- // Since the email string can come in a form of:
240
- // Name Last <name@example.com>
241
- // We have to extract just the email address, and discard the rest
242
- let tmp_to
243
- if (container?.to) {
244
- 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);
245
- if (matchResult) {
246
- tmp_to = matchResult[0].split('@');
247
- // Continue with your logic using tmp_to
248
- }
249
- }
250
-
251
- let tmp_from
252
- if (container?.from) {
253
- 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)
254
- if (matchResult) {
255
- tmp_from = matchResult[0].split('@');
256
- // Continue with your logic using tmp_to
257
- }
258
- }
259
-
260
- if (!tmp_to || !tmp_from)
261
- return reject(Error('No To or From field in the email'))
262
-
263
- // Get the domain name of the receiving end, so we can group
264
- // emails by all the domain that were added to SES.
265
- container.to_domain = tmp_to[1]
266
-
267
- // Based on the email name, we replace all the + characters, that
268
- // can be used to organize ones on-line accounts in to /, this way
269
- // we can build a S3 patch which will automatically organize
270
- // all the email in structured folder.
271
- container.to_account = tmp_to[0]?.replace(/\+/g, '/')
272
-
273
- // Get the domain name of the email which in our case will become the company name
274
- container.from_domain = tmp_from[1]
275
-
276
- // Get the name of who sent us the email
277
- container.from_account = tmp_from[0]
278
-
279
- // Set all the strings to lower case, because some times different
280
- // on-line services will have your email in all caps. For example
281
- // Priceline will do this.
282
- //
283
- // Since in the next step we compare the domain that is in SES
284
- // with the data from the email, wee need to have it all in the
285
- // same format for the if() statement to work correctly and
286
- // generate the right path where to save the email.
287
- container.to_domain = container.to_domain?.toLowerCase()
288
- container.to_account = container.to_account?.toLowerCase()
289
- container.from_domain = container.from_domain?.toLowerCase()
290
- container.from_account = container.from_account?.toLowerCase()
291
-
292
- // S3 objects have a limit of how they they can be named
293
- // so we remove everything but...
294
- container.subject = container.subject?.replace(/[^a-zA-Z0-9 &@:,$=+?;]/g, '_')
295
-
296
- return resolve(container)
297
- })
298
- }
299
-
300
- // Once we have the domains from SES, and we know the value of the To
301
- // field, we can decide where the emails should be saved.
302
- function where_to_save(container: Container): Promise<Container> {
303
- return new Promise((resolve, reject) => {
304
- log.info('where_to_save')
305
-
306
- // Since by default we assume that the email should be saved
307
- // in the Sent folder, we need to loop over the SES domains
308
- // and check if there is a match with the To: domain found in
309
- // the email.
310
- container.domains.forEach((domain) => {
311
- if (domain === container.to_domain)
312
- container.folder = 'Inbox'
313
- })
314
-
315
- return resolve(container)
316
- })
317
- }
318
-
319
- // Create Key path for the S3 object to be saved to.
320
- function create_s3_boject_key(container: Container): Promise<Container> {
321
- return new Promise((resolve, reject) => {
322
- log.info('create_s3_boject_key')
323
-
324
- // create the path where the email needs to be moved for house keeping
325
- container.path = `${container.to_domain}/${container.to_account}/${container.from_domain}/${container.from_account}/${container.date} - ${container.subject}/` + 'email.eml'
326
-
327
- return resolve(container)
328
- })
329
- }
330
-
331
- // Copy the email to a new location - we don't put the email that we
332
- // already have in memory since the system requires a COPY action and not
333
- // a PUT action.
334
- // TODO: refactor below
335
- // WARNING: We are using the escaped_key value, because there is a
336
- // known bug in the AWS JS SDK which won't unescape the
337
- // string, so you have to do it - AWS is aware of this issue.
338
- function copy_email_to_inbox(container: Container): Promise<Container> {
339
- return new Promise((resolve, reject) => {
340
- log.info('copy_email_to_inbox')
341
-
342
- const params = {
343
- Bucket: container.bucket,
344
- CopySource: `${container.bucket}/${container.escaped_key}`,
345
- Key: `${container.folder}/${container.path}`,
346
- }
347
-
348
- s3.copyObject(params, (error, data) => {
349
- if (error) {
350
- console.error(params)
351
- return reject(error)
352
- }
353
-
354
- return resolve(container)
355
- })
356
- })
357
- }
358
-
359
- // Similarly to the previous promise, but in this case we COPY the email to
360
- // the Today folder to showcase all the latest emails that were received
361
- // within 24h.
362
- //
363
- // There is a Lifecycle rule set on the bucket to delete any object inside
364
- // the Today folder if it is older then 24h.
365
- //
366
- // This way it is easier to see all the latest emails.
367
- function copy_email_to_today(container: Container): Promise<Container> {
368
- return new Promise((resolve, reject) => {
369
- log.info('copy_email_to_today')
370
-
371
- const params = {
372
- Bucket: container.bucket,
373
- CopySource: `${container.bucket}/${container.escaped_key}`,
374
- Key: `today/${container.path}`,
375
- }
376
-
377
- s3.copyObject(params, (error, data) => {
378
- if (error) {
379
- console.error(params)
380
- return reject(error)
381
- }
382
-
383
- return resolve(container)
384
- })
385
- })
386
- }
387
-
388
- function delete_the_email(container: Container): Promise<Container> {
389
- return new Promise((resolve, reject) => {
390
- log.info('delete_the_email')
391
-
392
- const params = {
393
- Bucket: container.bucket,
394
- Key: container.unescaped_key,
395
- }
396
-
397
- s3.deleteObject(params, (error, data) => {
398
- if (error) {
399
- console.error(params)
400
- return reject(error)
401
- }
402
-
403
- return resolve(container)
404
- })
405
- })
406
- }
1
+ // /* eslint-disable eslint-comments/no-unlimited-disable */
2
+ // /* eslint-disable */
3
+ // import type Stream from 'node:stream'
4
+ // import * as AWS from 'aws-sdk'
5
+ // import moment from 'moment'
6
+ // import { simpleParser as parser } from 'mailparser'
7
+ // import { log } from '@stacksjs/logging'
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 variable will hold all the domain available in SES so we don't
20
+ // // have to query SES each time the Lambda runs, we query SES only when
21
+ // // the lambda starts from scratch to circumvent the 1 sec request limit.
22
+ // const domains: string[] = []
23
+ //
24
+ // interface Container {
25
+ // bucket: string
26
+ // unescaped_key: string
27
+ // escaped_key: string
28
+ // domains: string[]
29
+ // folder: string
30
+ // // wip
31
+ // raw_email?: Buffer | Stream | string
32
+ // to?: string
33
+ // from?: string
34
+ // date?: Date | string
35
+ // subject?: string
36
+ // message_id?: string
37
+ // to_domain?: string
38
+ // to_account?: string
39
+ // from_domain?: string
40
+ // from_account?: string
41
+ // path?: string
42
+ // }
43
+ //
44
+ // interface S3Event {
45
+ // Records: {
46
+ // s3: {
47
+ // bucket: {
48
+ // name: string
49
+ // }
50
+ // object: {
51
+ // key: string
52
+ // }
53
+ // }
54
+ // }[]
55
+ // }
56
+ //
57
+ // // This Lambda will filter all the incoming emails based on their From and To field
58
+ // export function handler(event: S3Event): Promise<any> {
59
+ // // if (!event.Records.length)
60
+ // // throw new Error('No records in the event')
61
+ //
62
+ // // This JS object will contain all the data within the chain.
63
+ // const container: Container = {
64
+ // bucket: event.Records[0]?.s3.bucket.name || '',
65
+ // unescaped_key: '',
66
+ // escaped_key: event.Records[0]?.s3.object.key || '',
67
+ // domains,
68
+ // folder: 'Sent',
69
+ // }
70
+ //
71
+ // get_email_domains(container)
72
+ // .then((container) => {
73
+ // return unescape_key(container)
74
+ // }).then((container) => {
75
+ // return load_the_email(container)
76
+ // }).then((container) => {
77
+ // return parse_the_email(container)
78
+ // }).then((container) => {
79
+ // return format_time(container)
80
+ // }).then((container) => {
81
+ // return extract_data(container)
82
+ // }).then((container) => {
83
+ // return where_to_save(container)
84
+ // }).then((container) => {
85
+ // return create_s3_boject_key(container)
86
+ // }).then((container) => {
87
+ // return copy_email_to_inbox(container)
88
+ // }).then((container) => {
89
+ // return copy_email_to_today(container)
90
+ // }).then((container) => {
91
+ // return delete_the_email(container)
92
+ // }).then(() => {
93
+ // return true
94
+ // }).catch((error) => {
95
+ // console.error(error)
96
+ //
97
+ // return false
98
+ // })
99
+ //
100
+ // return Promise.resolve()
101
+ // }
102
+ //
103
+ // // List all the emails added to SES, so we can use them to decided where to
104
+ // // store the emails. If in the Inbox, or sent. This is important when you
105
+ // // upload emails by hand to back them up.
106
+ // //
107
+ // // Without doing this everything goes to the Inbox folder.
108
+ // function get_email_domains(container: Container): Promise<Container> {
109
+ // return new Promise((resolve, reject) => {
110
+ // // If we have already the domains we don't query SES anymore since you
111
+ // // can only query SES once a second. To solve this limitation
112
+ // // we grab the data once, and save it in to memory.
113
+ // if (container.domains.length)
114
+ // return resolve(container)
115
+ //
116
+ // log.info('get_email_domains')
117
+ //
118
+ // const params = {
119
+ // IdentityType: 'Domain',
120
+ // MaxItems: 100,
121
+ // }
122
+ //
123
+ // ses.listIdentities(params, (error, data) => {
124
+ // if (error) {
125
+ // console.error(params)
126
+ // return reject(error)
127
+ // }
128
+ //
129
+ // container.domains = data.Identities
130
+ //
131
+ // return resolve(container)
132
+ // })
133
+ // })
134
+ // }
135
+ //
136
+ // // We need to process the path received by S3 since AWS dose escape the
137
+ // // string in a special way. They escape the string in a HTML style
138
+ // // but for whatever reason they convert spaces in to +ses.
139
+ // function unescape_key(container: Container): Promise<Container> {
140
+ // return new Promise((resolve, reject) => {
141
+ // log.info('unescape_key')
142
+ //
143
+ // // First we convert the + in to spaces
144
+ // const plus_to_space = container.escaped_key.replace(/\+/g, ' ')
145
+ //
146
+ // // And then we unescape the string, other wise we lose real + characters
147
+ // const unescaped_key = decodeURIComponent(plus_to_space)
148
+ //
149
+ // container.unescaped_key = unescaped_key
150
+ //
151
+ // return resolve(container)
152
+ // })
153
+ // }
154
+ //
155
+ // // Load the email that we received from SES.
156
+ // function load_the_email(container: Container): Promise<Container> {
157
+ // return new Promise((resolve, reject) => {
158
+ // log.info('load_the_email')
159
+ //
160
+ // const params = {
161
+ // Bucket: container.bucket,
162
+ // Key: container.unescaped_key,
163
+ // }
164
+ //
165
+ // s3.getObject(params, (error, data) => {
166
+ // if (error) {
167
+ // console.error(params)
168
+ // return reject(error)
169
+ // }
170
+ //
171
+ // // Save the email for the next promise
172
+ // // @ts-expect-error - refactor later
173
+ // container.raw_email = data.Body
174
+ //
175
+ // return resolve(container)
176
+ // })
177
+ // })
178
+ // }
179
+ //
180
+ // // Once the raw email is loaded we parse it with one goal in mind, get
181
+ // // the date the of the email. This way we don't rely on the SES date, but
182
+ // // on the real date the email was created.
183
+ // //
184
+ // // This way we can even load in to the system old emails as long as they
185
+ // // are in the standard raw email format, and not some proprietary solution.
186
+ // //
187
+ // // That why will be organized with the time the emails were created, and not
188
+ // // received in to the system.
189
+ // function parse_the_email(container: Container): Promise<Container> {
190
+ // return new Promise((resolve, reject) => {
191
+ // log.info('parse_the_email')
192
+ //
193
+ // if (!container.raw_email)
194
+ // return reject(Error('No raw email to parse'))
195
+ //
196
+ // // Parse the email and extract all that is necessary
197
+ // parser(container.raw_email, (error, data) => {
198
+ // // Check for internal errors
199
+ // if (error) {
200
+ // console.error(data)
201
+ // return reject(error)
202
+ // }
203
+ //
204
+ // // Save the parsed email for the next promise.
205
+ // container.date = data.date
206
+ // container.from = data.from?.value[0]?.address
207
+ // // container.to = data.to?.value[0]?.address
208
+ // container.to = data.from?.value[0]?.address
209
+ // container.subject = data.subject || 'No Subject'
210
+ // container.message_id = data.messageId
211
+ //
212
+ // return resolve(container)
213
+ // })
214
+ // })
215
+ // }
216
+ //
217
+ // // We format the date and time to make sure that when the folder is saved
218
+ // // in S3, the object will sort one after the other which makes it much
219
+ // // easier to see the latest emails.
220
+ // function format_time(container: Container): Promise<Container> {
221
+ // return new Promise((resolve, reject) => {
222
+ // log.info('format_time')
223
+ //
224
+ // // Format the date found in the email message itself
225
+ // container.date = moment(container.date).format('YYYY-MM-DD HH:mm:ss Z')
226
+ //
227
+ // return resolve(container)
228
+ // })
229
+ // }
230
+ //
231
+ // // Extract all the data necessary to organize the incoming emails.
232
+ // function extract_data(container: Container): Promise<Container> {
233
+ // return new Promise((resolve, reject) => {
234
+ // log.info('extract_data')
235
+ //
236
+ // if (!container.to)
237
+ // return reject(Error('No To field in the email'))
238
+ //
239
+ // // Since the email string can come in a form of:
240
+ // // Name Last <name@example.com>
241
+ // // We have to extract just the email address, and discard the rest
242
+ // let tmp_to
243
+ // if (container?.to) {
244
+ // 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);
245
+ // if (matchResult) {
246
+ // tmp_to = matchResult[0].split('@');
247
+ // // Continue with your logic using tmp_to
248
+ // }
249
+ // }
250
+ //
251
+ // let tmp_from
252
+ // if (container?.from) {
253
+ // 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)
254
+ // if (matchResult) {
255
+ // tmp_from = matchResult[0].split('@');
256
+ // // Continue with your logic using tmp_to
257
+ // }
258
+ // }
259
+ //
260
+ // if (!tmp_to || !tmp_from)
261
+ // return reject(Error('No To or From field in the email'))
262
+ //
263
+ // // Get the domain name of the receiving end, so we can group
264
+ // // emails by all the domain that were added to SES.
265
+ // container.to_domain = tmp_to[1]
266
+ //
267
+ // // Based on the email name, we replace all the + characters, that
268
+ // // can be used to organize ones on-line accounts in to /, this way
269
+ // // we can build a S3 patch which will automatically organize
270
+ // // all the email in structured folder.
271
+ // container.to_account = tmp_to[0]?.replace(/\+/g, '/')
272
+ //
273
+ // // Get the domain name of the email which in our case will become the company name
274
+ // container.from_domain = tmp_from[1]
275
+ //
276
+ // // Get the name of who sent us the email
277
+ // container.from_account = tmp_from[0]
278
+ //
279
+ // // Set all the strings to lower case, because some times different
280
+ // // on-line services will have your email in all caps. For example
281
+ // // Priceline will do this.
282
+ // //
283
+ // // Since in the next step we compare the domain that is in SES
284
+ // // with the data from the email, wee need to have it all in the
285
+ // // same format for the if() statement to work correctly and
286
+ // // generate the right path where to save the email.
287
+ // container.to_domain = container.to_domain?.toLowerCase()
288
+ // container.to_account = container.to_account?.toLowerCase()
289
+ // container.from_domain = container.from_domain?.toLowerCase()
290
+ // container.from_account = container.from_account?.toLowerCase()
291
+ //
292
+ // // S3 objects have a limit of how they they can be named
293
+ // // so we remove everything but...
294
+ // container.subject = container.subject?.replace(/[^a-zA-Z0-9 &@:,$=+?;]/g, '_')
295
+ //
296
+ // return resolve(container)
297
+ // })
298
+ // }
299
+ //
300
+ // // Once we have the domains from SES, and we know the value of the To
301
+ // // field, we can decide where the emails should be saved.
302
+ // function where_to_save(container: Container): Promise<Container> {
303
+ // return new Promise((resolve, reject) => {
304
+ // log.info('where_to_save')
305
+ //
306
+ // // Since by default we assume that the email should be saved
307
+ // // in the Sent folder, we need to loop over the SES domains
308
+ // // and check if there is a match with the To: domain found in
309
+ // // the email.
310
+ // container.domains.forEach((domain) => {
311
+ // if (domain === container.to_domain)
312
+ // container.folder = 'Inbox'
313
+ // })
314
+ //
315
+ // return resolve(container)
316
+ // })
317
+ // }
318
+ //
319
+ // // Create Key path for the S3 object to be saved to.
320
+ // function create_s3_boject_key(container: Container): Promise<Container> {
321
+ // return new Promise((resolve, reject) => {
322
+ // log.info('create_s3_boject_key')
323
+ //
324
+ // // create the path where the email needs to be moved for house keeping
325
+ // container.path = `${container.to_domain}/${container.to_account}/${container.from_domain}/${container.from_account}/${container.date} - ${container.subject}/` + 'email.eml'
326
+ //
327
+ // return resolve(container)
328
+ // })
329
+ // }
330
+ //
331
+ // // Copy the email to a new location - we don't put the email that we
332
+ // // already have in memory since the system requires a COPY action and not
333
+ // // a PUT action.
334
+ // // TODO: refactor below
335
+ // // WARNING: We are using the escaped_key value, because there is a
336
+ // // known bug in the AWS JS SDK which won't unescape the
337
+ // // string, so you have to do it - AWS is aware of this issue.
338
+ // function copy_email_to_inbox(container: Container): Promise<Container> {
339
+ // return new Promise((resolve, reject) => {
340
+ // log.info('copy_email_to_inbox')
341
+ //
342
+ // const params = {
343
+ // Bucket: container.bucket,
344
+ // CopySource: `${container.bucket}/${container.escaped_key}`,
345
+ // Key: `${container.folder}/${container.path}`,
346
+ // }
347
+ //
348
+ // s3.copyObject(params, (error, data) => {
349
+ // if (error) {
350
+ // console.error(params)
351
+ // return reject(error)
352
+ // }
353
+ //
354
+ // return resolve(container)
355
+ // })
356
+ // })
357
+ // }
358
+ //
359
+ // // Similarly to the previous promise, but in this case we COPY the email to
360
+ // // the Today folder to showcase all the latest emails that were received
361
+ // // within 24h.
362
+ // //
363
+ // // There is a Lifecycle rule set on the bucket to delete any object inside
364
+ // // the Today folder if it is older then 24h.
365
+ // //
366
+ // // This way it is easier to see all the latest emails.
367
+ // function copy_email_to_today(container: Container): Promise<Container> {
368
+ // return new Promise((resolve, reject) => {
369
+ // log.info('copy_email_to_today')
370
+ //
371
+ // const params = {
372
+ // Bucket: container.bucket,
373
+ // CopySource: `${container.bucket}/${container.escaped_key}`,
374
+ // Key: `today/${container.path}`,
375
+ // }
376
+ //
377
+ // s3.copyObject(params, (error, data) => {
378
+ // if (error) {
379
+ // console.error(params)
380
+ // return reject(error)
381
+ // }
382
+ //
383
+ // return resolve(container)
384
+ // })
385
+ // })
386
+ // }
387
+ //
388
+ // function delete_the_email(container: Container): Promise<Container> {
389
+ // return new Promise((resolve, reject) => {
390
+ // log.info('delete_the_email')
391
+ //
392
+ // const params = {
393
+ // Bucket: container.bucket,
394
+ // Key: container.unescaped_key,
395
+ // }
396
+ //
397
+ // s3.deleteObject(params, (error, data) => {
398
+ // if (error) {
399
+ // console.error(params)
400
+ // return reject(error)
401
+ // }
402
+ //
403
+ // return resolve(container)
404
+ // })
405
+ // })
406
+ // }