lite-email-parser 1.0.2 → 1.0.3

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/README.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  Simple library to remove signature and replies and extract attachments and inline attachments from an email.
4
4
 
5
+ At [https://deerdesigner.com](Deer Designer) we were having trouble to receive emails and filter out unnecessary content. After a careful analysis, I noticed machine learning didn't deal well with HTML mixed with attachments and embedded files. Our use case depends on enriched HTML and there weren't any good libs out there that solve this problem. So I decided to create this library.
6
+
7
+ If you stumble upon an email that is not parsed correctly, please create an issue and attach the raw .eml file.
8
+
9
+ Example of a parsed email: [sample](./sample/sample.ts)
10
+
5
11
  ## Import
6
12
 
7
13
  `const { parseEmail } = require('lite-email-parser');`
@@ -36,8 +42,8 @@ console.log(result.inlineAttachments);
36
42
  ### File upload
37
43
 
38
44
  ```
39
- const { parseEmail } = require('lite-email-parser');
40
- const fs = require('fs');
45
+ import { parseEmail, replaceSrc } from 'lite-email-parser';
46
+ import * as fs from 'fs';
41
47
 
42
48
  // Load your email (string or Buffer accepted here)
43
49
  const email = fs.readFileSync('email.eml');
@@ -1 +1 @@
1
- {"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../../src/parser.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAErD,wBAAsB,UAAU,CAC9B,MAAM,EAAE,MAAM,GAAG,MAAM,GACtB,OAAO,CAAC,gBAAgB,CAAC,CAkB3B;AAuDD,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,MAAM,CAW/D"}
1
+ {"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../../src/parser.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAErD,wBAAsB,UAAU,CAC9B,MAAM,EAAE,MAAM,GAAG,MAAM,GACtB,OAAO,CAAC,gBAAgB,CAAC,CAsC3B;AAKD,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,MAAM,CAW/D"}
@@ -44,55 +44,31 @@ async function parseEmail(buffer) {
44
44
  content = parsedEmail.text || '';
45
45
  }
46
46
  const cleanedContent = extractFirstMessage(content);
47
- const attachments = await attachmentsToFiles(parsedEmail.attachments || []);
48
- const inlineAttachments = inlineAttachmentsToFiles(cleanedContent);
49
- return {
50
- lastMessage: cleanedContent,
51
- attachments,
52
- inlineAttachments,
53
- };
54
- }
55
- async function attachmentsToFiles(attach) {
56
- const files = [];
57
- for (const att of attach) {
47
+ const attachments = [];
48
+ const inlineAttachments = [];
49
+ for (const att of parsedEmail.attachments || []) {
58
50
  const filename = att.filename || new Date().toDateString();
59
51
  const contentType = att.contentType || 'application/octet-stream';
60
- const attachment = {
52
+ const file = {
61
53
  name: filename,
62
54
  type: contentType,
63
55
  size: att.size,
64
56
  buffer: att.content,
65
57
  };
66
- files.push(attachment);
67
- }
68
- return files;
69
- }
70
- function inlineAttachmentsToFiles(content) {
71
- const files = [];
72
- const $ = cheerio.load(content);
73
- // Base64 embedded images — extract them as IFile entries
74
- const base64Images = $('img[src^="data:"]').toArray();
75
- for (const el of base64Images) {
76
- const src = $(el).attr('src');
77
- if (!src)
78
- continue;
79
- const matches = src.match(/^data:(image\/(\w+));base64,(.*)$/);
80
- if (matches) {
81
- const contentType = matches[1];
82
- const extension = matches[2];
83
- const base64Data = matches[3];
84
- const buffer = Buffer.from(base64Data, 'base64');
85
- files.push({
86
- name: `inline-${new Date().toISOString()}.${extension}`,
87
- src: src, // if we upload to cloud, replace src
88
- type: contentType,
89
- size: buffer.byteLength,
90
- buffer,
91
- originalSrc: src,
92
- });
58
+ if (att.related) {
59
+ const base64Data = att.content.toString('base64');
60
+ file.originalSrc = `data:${contentType};base64,${base64Data}`;
61
+ inlineAttachments.push(file);
62
+ }
63
+ else {
64
+ attachments.push(file);
93
65
  }
94
66
  }
95
- return files;
67
+ return {
68
+ lastMessage: cleanedContent,
69
+ attachments,
70
+ inlineAttachments,
71
+ };
96
72
  }
97
73
  // Replace src attributes with the uploaded file URLs
98
74
  function replaceSrc(html, files) {
@@ -1 +1 @@
1
- {"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../src/parser.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAErD,wBAAsB,UAAU,CAC9B,MAAM,EAAE,MAAM,GAAG,MAAM,GACtB,OAAO,CAAC,gBAAgB,CAAC,CAkB3B;AAuDD,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,MAAM,CAW/D"}
1
+ {"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../src/parser.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAErD,wBAAsB,UAAU,CAC9B,MAAM,EAAE,MAAM,GAAG,MAAM,GACtB,OAAO,CAAC,gBAAgB,CAAC,CAsC3B;AAKD,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,MAAM,CAW/D"}
package/dist/parser.js CHANGED
@@ -7,55 +7,31 @@ export async function parseEmail(buffer) {
7
7
  content = parsedEmail.text || '';
8
8
  }
9
9
  const cleanedContent = extractFirstMessage(content);
10
- const attachments = await attachmentsToFiles(parsedEmail.attachments || []);
11
- const inlineAttachments = inlineAttachmentsToFiles(cleanedContent);
12
- return {
13
- lastMessage: cleanedContent,
14
- attachments,
15
- inlineAttachments,
16
- };
17
- }
18
- async function attachmentsToFiles(attach) {
19
- const files = [];
20
- for (const att of attach) {
10
+ const attachments = [];
11
+ const inlineAttachments = [];
12
+ for (const att of parsedEmail.attachments || []) {
21
13
  const filename = att.filename || new Date().toDateString();
22
14
  const contentType = att.contentType || 'application/octet-stream';
23
- const attachment = {
15
+ const file = {
24
16
  name: filename,
25
17
  type: contentType,
26
18
  size: att.size,
27
19
  buffer: att.content,
28
20
  };
29
- files.push(attachment);
30
- }
31
- return files;
32
- }
33
- function inlineAttachmentsToFiles(content) {
34
- const files = [];
35
- const $ = cheerio.load(content);
36
- // Base64 embedded images — extract them as IFile entries
37
- const base64Images = $('img[src^="data:"]').toArray();
38
- for (const el of base64Images) {
39
- const src = $(el).attr('src');
40
- if (!src)
41
- continue;
42
- const matches = src.match(/^data:(image\/(\w+));base64,(.*)$/);
43
- if (matches) {
44
- const contentType = matches[1];
45
- const extension = matches[2];
46
- const base64Data = matches[3];
47
- const buffer = Buffer.from(base64Data, 'base64');
48
- files.push({
49
- name: `inline-${new Date().toISOString()}.${extension}`,
50
- src: src, // if we upload to cloud, replace src
51
- type: contentType,
52
- size: buffer.byteLength,
53
- buffer,
54
- originalSrc: src,
55
- });
21
+ if (att.related) {
22
+ const base64Data = att.content.toString('base64');
23
+ file.originalSrc = `data:${contentType};base64,${base64Data}`;
24
+ inlineAttachments.push(file);
25
+ }
26
+ else {
27
+ attachments.push(file);
56
28
  }
57
29
  }
58
- return files;
30
+ return {
31
+ lastMessage: cleanedContent,
32
+ attachments,
33
+ inlineAttachments,
34
+ };
59
35
  }
60
36
  // Replace src attributes with the uploaded file URLs
61
37
  export function replaceSrc(html, files) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lite-email-parser",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Parse email to get signature, replies, attachments and inline images",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/index.js",
@@ -27,6 +27,27 @@
27
27
  },
28
28
  "author": "philipedc",
29
29
  "type": "module",
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "git+https://github.com/philipedc/lite-email-parser.git"
33
+ },
34
+ "bugs": {
35
+ "url": "https://github.com/philipedc/lite-email-parser/issues"
36
+ },
37
+ "homepage": "https://github.com/philipedc/lite-email-parser#readme",
38
+ "keywords": [
39
+ "email",
40
+ "parser",
41
+ "email-parser",
42
+ "mailparser",
43
+ "extract-attachments",
44
+ "remove-signature",
45
+ "remove-replies",
46
+ "inline-images",
47
+ "lite-email-parser",
48
+ "email-to-html",
49
+ "signature-remover"
50
+ ],
30
51
  "dependencies": {
31
52
  "cheerio": "^1.2.0",
32
53
  "mailparser": "^3.9.14"