@stacksjs/email 0.66.0 → 0.68.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/config.d.ts +3 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +0 -3
- package/dist/template.d.ts +6 -5
- package/dist/types.d.ts +35 -31
- package/package.json +7 -9
- package/dist/drivers/index.d.ts +0 -1
- package/dist/drivers/mailgun.d.ts +0 -0
- package/dist/drivers/nodemailer.d.ts +0 -0
- package/dist/drivers/ses.d.ts +0 -0
- package/dist/email.d.ts +0 -0
- package/dist/index.js.map +0 -9
- package/dist/send.d.ts +0 -0
- package/dist/server/converter.d.ts +0 -0
- package/dist/server/inbound.d.ts +0 -0
- package/dist/server/outbound.d.ts +0 -0
- package/dist/utils/config.d.ts +0 -3
- 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/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
|
-
// }
|