-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodejs_email_parser_example.js
More file actions
363 lines (308 loc) · 11.4 KB
/
nodejs_email_parser_example.js
File metadata and controls
363 lines (308 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/env node
/**
* Email Parser Example - Node.js Implementation
* Demonstrates parsing HTML emails and extracting structured data
*/
const { simpleParser } = require('mailparser');
const cheerio = require('cheerio');
const fs = require('fs').promises;
const path = require('path');
const csv = require('csv-parse/sync');
const xlsx = require('xlsx');
class EmailParser {
constructor() {
this.vendorConfigs = {
amazon: {
identifiers: ['amazon.com', 'Your order has been dispatched'],
patterns: {
orderId: /Order #\s*(\d{3}-\d{7}-\d{7})/i,
total: /Order Total:\s*\$([0-9,]+\.\d{2})/i,
deliveryDate: /Arriving:\s*([A-Za-z]+\s+\d+)/i,
tracking: /Tracking ID:\s*(\w+)/i
},
selectors: {
items: 'table.shipment tr',
address: 'div.ship-to-address'
}
},
shopify: {
identifiers: ['Order confirmation', 'Thank you for your purchase'],
patterns: {
orderId: /Order\s*#(\d+)/i,
total: /Total\s*\$([0-9,]+\.\d{2})/i,
customerName: /Hi\s+([^,]+),/i,
email: /[\w\.-]+@[\w\.-]+\.\w+/
},
selectors: {
items: '.order-list__item',
shipping: '.shipping-address'
}
},
generic: {
identifiers: [],
patterns: {
orderId: /Order\s*(?:#|Number|ID)?\s*[:]\s*(\w+)/i,
total: /(?:Total|Amount|Grand Total):?\s*\$([0-9,]+\.\d{2})/i,
date: /(?:Date|Order Date):?\s*(\d{1,2}[/-]\d{1,2}[/-]\d{2,4})/i,
email: /[\w\.-]+@[\w\.-]+\.\w+/,
phone: /(?:Phone|Tel):?\s*\+?(\d[\d\s\-\(\)]+)/i
},
selectors: {
items: 'table',
anyTable: 'table'
}
}
};
}
async parseRawEmail(rawEmail) {
try {
const parsed = await simpleParser(rawEmail);
const result = {
headers: {
subject: parsed.subject || '',
from: parsed.from ? parsed.from.text : '',
to: parsed.to ? parsed.to.text : '',
date: parsed.date || ''
},
body: {
html: parsed.html || null,
text: parsed.text || null
},
attachments: []
};
// Process attachments
if (parsed.attachments && parsed.attachments.length > 0) {
for (const attachment of parsed.attachments) {
result.attachments.push({
filename: attachment.filename,
contentType: attachment.contentType,
size: attachment.size,
content: attachment.content
});
}
}
return result;
} catch (error) {
throw new Error(`Failed to parse email: ${error.message}`);
}
}
detectVendor(content) {
const contentLower = content.toLowerCase();
for (const [vendor, config] of Object.entries(this.vendorConfigs)) {
if (vendor === 'generic') continue;
for (const identifier of config.identifiers) {
if (contentLower.includes(identifier.toLowerCase())) {
return vendor;
}
}
}
return 'generic';
}
extractData(htmlContent) {
const $ = cheerio.load(htmlContent);
const vendor = this.detectVendor(htmlContent);
const config = this.vendorConfigs[vendor];
const result = {
vendor: vendor,
extractedData: {},
items: [],
tables: [],
rawText: $.text()
};
// Extract using patterns
for (const [field, pattern] of Object.entries(config.patterns)) {
const match = result.rawText.match(pattern);
if (match) {
result.extractedData[field] = match[1].trim();
}
}
// Extract using selectors
for (const [field, selector] of Object.entries(config.selectors)) {
const elements = $(selector);
if (elements.length > 0) {
if (field === 'items') {
result.items = this._parseItems($, elements);
} else {
result.extractedData[field] = elements.map((i, el) => $(el).text().trim()).get();
}
}
}
// Extract all tables
result.tables = this._extractTables($);
return result;
}
_parseItems($, elements) {
const items = [];
elements.each((i, elem) => {
const $elem = $(elem);
const item = {};
// Try to extract common fields
const nameElem = $elem.find('[class*="product"], [class*="item"], [class*="name"]').first();
if (nameElem.length) {
item.name = nameElem.text().trim();
}
const priceElem = $elem.find('[class*="price"], [class*="cost"], [class*="amount"]').first();
if (priceElem.length) {
item.price = priceElem.text().trim();
}
const qtyElem = $elem.find('[class*="qty"], [class*="quantity"], [class*="count"]').first();
if (qtyElem.length) {
item.quantity = qtyElem.text().trim();
}
// If no specific fields found, just get all text
if (Object.keys(item).length === 0) {
item.text = $elem.text().trim();
}
if (Object.keys(item).length > 0) {
items.push(item);
}
});
return items;
}
_extractTables($) {
const tables = [];
$('table').each((i, table) => {
const tableData = this._parseTable($, table);
if (tableData && tableData.length > 0) {
tables.push(tableData);
}
});
return tables;
}
_parseTable($, table) {
const rows = [];
const $table = $(table);
$table.find('tr').each((i, row) => {
const cells = [];
$(row).find('td, th').each((j, cell) => {
cells.push($(cell).text().trim());
});
if (cells.length > 0) {
rows.push(cells);
}
});
return rows;
}
async parseAttachment(content, filename) {
if (filename.endsWith('.csv')) {
return csv.parse(content, {
columns: true,
skip_empty_lines: true
});
} else if (filename.endsWith('.xlsx') || filename.endsWith('.xls')) {
const workbook = xlsx.read(content, { type: 'buffer' });
const sheetName = workbook.SheetNames[0];
return xlsx.utils.sheet_to_json(workbook.Sheets[sheetName]);
} else if (filename.endsWith('.json')) {
return JSON.parse(content.toString());
} else {
return content;
}
}
}
class OrderDataExtractor {
static normalizePrice(priceStr) {
if (!priceStr) return 0.0;
// Remove currency symbols and commas
const cleanPrice = priceStr.replace(/[^\d.]/g, '');
try {
return parseFloat(cleanPrice);
} catch (error) {
return 0.0;
}
}
static parseDate(dateStr) {
const dateFormats = [
{ regex: /(\d{1,2})\/(\d{1,2})\/(\d{4})/, format: 'MM/DD/YYYY' },
{ regex: /(\d{1,2})-(\d{1,2})-(\d{4})/, format: 'MM-DD-YYYY' },
{ regex: /(\d{4})-(\d{1,2})-(\d{1,2})/, format: 'YYYY-MM-DD' },
{ regex: /([A-Za-z]+)\s+(\d{1,2}),\s+(\d{4})/, format: 'Month DD, YYYY' }
];
for (const { regex } of dateFormats) {
const match = dateStr.match(regex);
if (match) {
return new Date(dateStr);
}
}
return null;
}
static extractAddress(text) {
const address = {};
// ZIP code pattern
const zipMatch = text.match(/\b\d{5}(?:-\d{4})?\b/);
if (zipMatch) {
address.zip = zipMatch[0];
}
// State pattern (US)
const stateMatch = text.match(/\b[A-Z]{2}\b/);
if (stateMatch) {
address.state = stateMatch[0];
}
// City (word before state)
if (address.state) {
const cityMatch = text.match(new RegExp(`(\\w+)\\s+${address.state}`));
if (cityMatch) {
address.city = cityMatch[1];
}
}
return address;
}
}
// Utility functions
async function parseEmailFile(filePath) {
const parser = new EmailParser();
try {
// Read email file
const rawEmail = await fs.readFile(filePath);
// Parse email
const emailData = await parser.parseRawEmail(rawEmail);
// Extract order data from HTML body
if (emailData.body.html) {
const orderData = parser.extractData(emailData.body.html);
console.log(`Vendor: ${orderData.vendor}`);
console.log(`Order ID: ${orderData.extractedData.orderId || 'Not found'}`);
console.log(`Total: ${orderData.extractedData.total || 'Not found'}`);
console.log(`Items found: ${orderData.items.length}`);
// Process tables
orderData.tables.forEach((table, index) => {
console.log(`\nTable ${index + 1}:`);
console.table(table.slice(0, 5)); // Show first 5 rows
});
// Process attachments
if (emailData.attachments.length > 0) {
console.log(`\nAttachments: ${emailData.attachments.length}`);
for (const attachment of emailData.attachments) {
console.log(`- ${attachment.filename} (${attachment.size} bytes)`);
// Parse attachment if supported
try {
const parsedData = await parser.parseAttachment(
attachment.content,
attachment.filename
);
console.log(` Parsed ${attachment.filename}:`, parsedData);
} catch (err) {
console.log(` Could not parse ${attachment.filename}: ${err.message}`);
}
}
}
}
return emailData;
} catch (error) {
console.error('Error parsing email:', error);
throw error;
}
}
// Export modules
module.exports = {
EmailParser,
OrderDataExtractor,
parseEmailFile
};
// Example usage
if (require.main === module) {
// Example: Parse an email file
const emailPath = process.argv[2] || 'sample_order_email.eml';
parseEmailFile(emailPath)
.then(() => console.log('\nEmail parsing complete!'))
.catch(err => console.error('Failed to parse email:', err));
}