email-forward-parser 1.0.14 → 1.0.15

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/lib/parser.js CHANGED
@@ -5,7 +5,7 @@ var RE2 = require("re2");
5
5
  var { loopRegexes, trimString } = require("./utils");
6
6
 
7
7
 
8
- var MULTIPLE_MAILBOXES_SEPARATORS = [
8
+ var MAILBOXES_SEPARATORS = [
9
9
  ",", // Apple Mail, Gmail, New Outlook 2019, Thunderbird
10
10
  ";" // Outlook Live, Yahoo Mail
11
11
  ];
@@ -325,12 +325,14 @@ var REGEXES = {
325
325
  /\s*Відправлено\s?:(.+)$/m // Yahoo Mail (uk)
326
326
  ],
327
327
 
328
- name_address : [
329
- /^(.+)\s?\n?\s*[\[|<]mailto\:(.+)[\]|>]$/, // "Walter Sheltan <mailto:walter.sheltan@acme.com>" or "Walter Sheltan [mailto:walter.sheltan@acme.com]" or "walter.sheltan@acme.com <mailto:walter.sheltan@acme.com>"
330
- /^\"(.+)\"\s?\n?\s*[\[|<](.+)[\]|>]$/, // ""Walter Sheltan" <walter.sheltan@acme.com>" or ""Walter Sheltan" [walter.sheltan@acme.com]" or ""walter.sheltan@acme.com" <walter.sheltan@acme.com>"
331
- /^(.+)\s?\n?\s*[\[|<](.+)[\]|>]$/, // "Walter Sheltan <walter.sheltan@acme.com>" or "Walter Sheltan [walter.sheltan@acme.com]" or "walter.sheltan@acme.com <walter.sheltan@acme.com>"
332
- /^(.?)\s?\n?\s*[\[|<](.+)[\]|>]$/, // "<walter.sheltan@acme.com>"
333
- /(.?)\s?\n?\s*[\[|<](.+)[\]|>]/ // "<walter.sheltan@acme.com>" (lax)
328
+ mailbox : [
329
+ /^(.+?)\s?\n?\s*[\[|<]mailto\:(.+?)[\]|>]/, // "Walter Sheltan <mailto:walter.sheltan@acme.com>" or "Walter Sheltan [mailto:walter.sheltan@acme.com]" or "walter.sheltan@acme.com <mailto:walter.sheltan@acme.com>"
330
+ /^\'(.+?)\'\s?\n?\s*[\[|<](.+?)[\]|>]/, // "'Walter Sheltan' <walter.sheltan@acme.com>" or "'Walter Sheltan' [walter.sheltan@acme.com]" or "'walter.sheltan@acme.com' <walter.sheltan@acme.com>"
331
+ /^\"\'(.+?)\'\"\s?\n?\s*[\[|<](.+?)[\]|>]/, // ""'Walter Sheltan'" <walter.sheltan@acme.com>" or ""'Walter Sheltan'" [walter.sheltan@acme.com]" or ""'walter.sheltan@acme.com'" <walter.sheltan@acme.com>"
332
+ /^\"(.+?)\"\s?\n?\s*[\[|<](.+?)[\]|>]/, // ""Walter Sheltan" <walter.sheltan@acme.com>" or ""Walter Sheltan" [walter.sheltan@acme.com]" or ""walter.sheltan@acme.com" <walter.sheltan@acme.com>"
333
+ /^([^,;]+?)\s?\n?\s*[\[|<](.+?)[\]|>]/, // "Walter Sheltan <walter.sheltan@acme.com>" or "Walter Sheltan [walter.sheltan@acme.com]" or "walter.sheltan@acme.com <walter.sheltan@acme.com>"
334
+ /^(.?)\s?\n?\s*[\[|<](.+?)[\]|>]/, // "<walter.sheltan@acme.com>"
335
+ /^([^\s@]+@[^\s@]+\.[^\s@,]+)/ // "walter.sheltan@acme.com"
334
336
  ]
335
337
  };
336
338
 
@@ -708,37 +710,61 @@ class Parser {
708
710
  var _mailboxesLine = trimString(_match[_match.length - 1]);
709
711
 
710
712
  if (_mailboxesLine) {
711
- var _mailboxes = loopRegexes(
712
- MULTIPLE_MAILBOXES_SEPARATORS,
713
- _mailboxesLine,
714
-
715
- "split"
716
- );
717
-
718
- // Extract name and address (single / multiple mailboxes)
719
- for (var _i = 0; _i < _mailboxes.length; _i++) {
720
- var _nameAddressMatch = loopRegexes(
721
- this.__regexes.name_address,
722
- trimString(_mailboxes[_i])
713
+ var _mailboxes = [];
714
+
715
+ while (_mailboxesLine) {
716
+ var _mailboxMatch = loopRegexes(
717
+ this.__regexes.mailbox,
718
+ _mailboxesLine
723
719
  );
724
720
 
725
- // Name and address available?
726
- if (_nameAddressMatch && _nameAddressMatch.length > 0) {
727
- var _address = trimString(_nameAddressMatch[2]);
728
- var _name = trimString(_nameAddressMatch[1]);
721
+ // Address and / or name available?
722
+ if (_mailboxMatch && _mailboxMatch.length > 0) {
723
+ var _address = null;
724
+ var _name = null;
725
+
726
+ // Address and name available?
727
+ if (_mailboxMatch.length === 3) {
728
+ _address = trimString(_mailboxMatch[2]);
729
+ _name = trimString(_mailboxMatch[1]);
730
+ } else {
731
+ _address = trimString(_mailboxMatch[1]);
732
+ }
729
733
 
730
- _mailboxes[_i] = {
734
+ _mailboxes.push({
731
735
  address : _address,
732
736
 
733
737
  // Some clients fill the name with the address \
734
738
  // ("bessie.berry@acme.com <bessie.berry@acme.com>")
735
739
  name : (_address !== _name) ? _name : null
736
- };
740
+ });
741
+
742
+ // Remove matched mailbox from mailboxes line
743
+ _mailboxesLine = trimString(
744
+ _mailboxesLine.replace(_mailboxMatch[0], "")
745
+ );
746
+
747
+ if (_mailboxesLine) {
748
+ // Remove leading mailboxes separator \
749
+ // (", Nicholas <nicholas@globex.corp>")
750
+ for (var _i = 0; _i < MAILBOXES_SEPARATORS.length; _i++) {
751
+ let _separator = MAILBOXES_SEPARATORS[_i];
752
+
753
+ if (_mailboxesLine[0] === _separator) {
754
+ _mailboxesLine = trimString(_mailboxesLine.substring(1));
755
+
756
+ break;
757
+ }
758
+ }
759
+ }
737
760
  } else {
738
- _mailboxes[_i] = {
739
- address : trimString(_mailboxes[_i]),
761
+ _mailboxes.push({
762
+ address : trimString(_mailboxesLine),
740
763
  name : null
741
- };
764
+ });
765
+
766
+ // No more matches
767
+ _mailboxesLine = "";
742
768
  }
743
769
  }
744
770
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "email-forward-parser",
3
- "version": "1.0.14",
3
+ "version": "1.0.15",
4
4
  "description": "Parses forwarded emails and extract content",
5
5
  "author": "Eliott Vincent <eliott@crisp.chat>",
6
6
  "main": "lib/index.js",
package/result+1.txt ADDED
@@ -0,0 +1,51 @@
1
+
2
+ --
3
+
4
+ Baptiste Jamin
5
+ CEO, Crisp <https://crisp.im/> - Connect your customers to your team.
6
+
7
+ > Begin forwarded message:
8
+ >
9
+ > From: "Grant, Jordan" <Jordan.Grant@slam.nhs.uk>
10
+ > Subject: Re: [ACTION REQUIRED] Your Crisp subscription is about to be cancelled
11
+ > Date: 9 May 2022 at 14:09:04 CEST
12
+ > To: "'billing@crisp.chat'" <billing@crisp.chat>, "'baptiste@crisp.chat'" <baptiste@crisp.chat>
13
+ > Cc: "Winter, Helen" <Helen.Winter@slam.nhs.uk>, "Pollard, Clair" <Clair.Pollard@slam.nhs.uk>, "Wilson, Hannah" <Hannah.Wilson2@slam.nhs.uk>
14
+ >
15
+ > Hi Billings team,
16
+ >
17
+ > My name is Jordan Grant and I am emailing on behalf of Helen winter in regards to the CRISP subscription.
18
+ >
19
+ > We still would like to continue this subscription but will need more time to make payment. As we have a process we need to go through on our side.
20
+ >
21
+ > Apologies for the delay in getting back you sooner.
22
+ >
23
+ > Helen, we could not renew your subscriptions.
24
+ > We are still awaiting for the payment due on your Crisp account.
25
+ > Please check your unpaid invoices in your Crisp settings, and proceed the manual payment if you did not do so yet.
26
+ > Provider:
27
+ > WIRE
28
+ > Payment Details:
29
+ > GB 1234XXX
30
+ > Expiration:
31
+ > 05/22
32
+ > Review my billing settings <https://hooks.crisp.chat/email/link/notification/?user_id=4f8a3219-b77d-47fd-b754-0b9d39fd3bb7&type=billing_charge_error&link_url=https%3A%2F%2Fapp.crisp.chat%2Fsettings%2Fbilling%2F&signature=72098c797340a00>
33
+ > Note that if we do not receive the funds within a week, we will have to cancel your subscription. If you need more time, you can still contact us and ask us to extend the payment delay.
34
+ > You receive this email because we could not renew some active subscriptions on Crisp.
35
+ > Crisp website <https://crisp.chat/>
36
+ > Documentation <https://help.crisp.chat/>
37
+ > Unsubscribe from emails <https://app.crisp.chat/settings/notifications/>
38
+ >
39
+ > We run on
40
+ >
41
+ >
42
+ >
43
+ >
44
+ >
45
+ > Jordan Grant
46
+ > Business Manager, Corporate Psychology and Psychotherapy
47
+ > South London and Maudsley NHS Foundation Trust
48
+ > The Maudsley Hospital
49
+ > Email: jordan.grant@slam.nhs.uk <mailto:jordan.grant@slam.nhs.uk>
50
+ > 020 3228 6000 The switchboard number for SLaM
51
+
package/test-regex.js ADDED
@@ -0,0 +1,123 @@
1
+ var RE2 = require("re2");
2
+ var { loopRegexes, trimString } = require("./lib/utils");
3
+
4
+
5
+ var MAILBOXES_SEPARATORS = [
6
+ ",", // Apple Mail, Gmail, New Outlook 2019, Thunderbird
7
+ ";" // Outlook Live, Yahoo Mail
8
+ ];
9
+
10
+ this.__regexes = [];
11
+
12
+ var REGEXES = {
13
+ mailbox : [
14
+ /(.+?)\s?\n?\s*[\[|<]mailto\:(.+?)[\]|>]/, // "Walter Sheltan <mailto:walter.sheltan@acme.com>" or "Walter Sheltan [mailto:walter.sheltan@acme.com]" or "walter.sheltan@acme.com <mailto:walter.sheltan@acme.com>"
15
+ /\'(.+?)\'\s?\n?\s*[\[|<](.+?)[\]|>]/, // "'Walter Sheltan' <walter.sheltan@acme.com>" or "'Walter Sheltan' [walter.sheltan@acme.com]" or "'walter.sheltan@acme.com' <walter.sheltan@acme.com>"
16
+ /\"\'(.+?)\'\"\s?\n?\s*[\[|<](.+?)[\]|>]/, // ""'Walter Sheltan'" <walter.sheltan@acme.com>" or ""'Walter Sheltan'" [walter.sheltan@acme.com]" or ""'walter.sheltan@acme.com'" <walter.sheltan@acme.com>"
17
+ /\"(.+?)\"\s?\n?\s*[\[|<](.+?)[\]|>]/, // ""Walter Sheltan" <walter.sheltan@acme.com>" or ""Walter Sheltan" [walter.sheltan@acme.com]" or ""walter.sheltan@acme.com" <walter.sheltan@acme.com>"
18
+ /([^\,\;]+?)\s?\n?\s*[\[|<](.+?)[\]|>]/, // "Walter Sheltan <walter.sheltan@acme.com>" or "Walter Sheltan [walter.sheltan@acme.com]" or "walter.sheltan@acme.com <walter.sheltan@acme.com>"
19
+ /(.?)\s?\n?\s*[\[|<](.+?)[\]|>]/, // "<walter.sheltan@acme.com>" or "[walter.sheltan@acme.com]"
20
+ /([^\s@]+@[^\s@]+\.[^\s@,]+)/ // "walter.sheltan@acme.com"
21
+ ]
22
+ };
23
+
24
+ for (var _key in REGEXES) {
25
+ var _entry = REGEXES[_key];
26
+
27
+ if (Array.isArray(_entry)) {
28
+ this.__regexes[_key] = [];
29
+
30
+ for (var _i = 0; _i < _entry.length; _i++) {
31
+ this.__regexes[_key].push(new RE2(_entry[_i]));
32
+ }
33
+ } else {
34
+ this.__regexes[_key] = new RE2(_entry);
35
+ }
36
+ }
37
+
38
+
39
+ /**
40
+ * Parses mailboxes(s)
41
+ * @private
42
+ * @param {object} regexes
43
+ * @param {string} text
44
+ * @param {boolean} [force_array]
45
+ * @return {object} The parsed mailboxes(s)
46
+ */
47
+ var __parseMailbox = (regexes, text, force_array=false) => {
48
+ var _mailboxesLine = trimString("walter.sheltan@acme.com, Nicholas <nicholas@globex.corp>");
49
+ // var _mailboxesLine = trimString("bessie.berry@acme.com, suzanne@globex.corp");
50
+ // var _mailboxesLine = trimString("Walter Sheltan <walter.sheltan@acme.com>, Nicholas <nicholas@globex.corp>");
51
+
52
+ if (_mailboxesLine) {
53
+ var _mailboxes = [];
54
+
55
+ while (_mailboxesLine) {
56
+ var _mailboxMatch = loopRegexes(
57
+ this.__regexes.mailbox,
58
+ _mailboxesLine
59
+ );
60
+
61
+ // Name and address available?
62
+ if (_mailboxMatch && _mailboxMatch.length > 0) {
63
+ var _address = null;
64
+ var _name = null;
65
+
66
+ // Only address available?
67
+ if (_mailboxMatch.length === 2) {
68
+ _address = trimString(_mailboxMatch[1]);
69
+ } else {
70
+ _address = trimString(_mailboxMatch[2]);
71
+ _name = trimString(_mailboxMatch[1]);
72
+ }
73
+
74
+ _mailboxes.push({
75
+ address : _address,
76
+
77
+ // Some clients fill the name with the address \
78
+ // ("bessie.berry@acme.com <bessie.berry@acme.com>")
79
+ name : (_address !== _name) ? _name : null
80
+ });
81
+
82
+ // Remove matched mailbox from mailboxes line
83
+ _mailboxesLine = trimString(
84
+ _mailboxesLine.replace(_mailboxMatch[0], "")
85
+ );
86
+
87
+ if (_mailboxesLine) {
88
+ // Remove trailing mailboxes separator
89
+ for (var _i = 0; _i < MAILBOXES_SEPARATORS.length; _i++) {
90
+ let _separator = MAILBOXES_SEPARATORS[_i];
91
+
92
+ if (_mailboxesLine[0] === _separator) {
93
+ _mailboxesLine = trimString(_mailboxesLine.substring(1));
94
+
95
+ break;
96
+ }
97
+ }
98
+ }
99
+ } else {
100
+ _mailboxes.push({
101
+ address : trimString(_mailboxesLine),
102
+ name : null
103
+ });
104
+
105
+ // No more matches
106
+ _mailboxesLine = "";
107
+ }
108
+ }
109
+
110
+ // Return multiple mailboxes
111
+ if (_mailboxes.length > 1) {
112
+ return _mailboxes;
113
+ }
114
+
115
+ // Return single mailbox
116
+ return (force_array === true) ? _mailboxes : _mailboxes[0];
117
+ }
118
+ }
119
+
120
+
121
+ console.log(
122
+ __parseMailbox("Walter Sheltan <walter.sheltan@acme.com>, Nicholas <nicholas@globex.corp>")
123
+ );
package/test.js CHANGED
@@ -1,15 +1,15 @@
1
1
  const EmailForwardParser = require("./");
2
2
  const fs = require("fs");
3
3
 
4
- MY_EMAIL_STRING = fs.readFileSync("./test.txt", "utf-8");
5
- // MY_EMAIL_STRING = fs.readFileSync("./test_working.txt", "utf-8");
6
- // const result = new EmailForwardParser().read(MY_EMAIL_STRING, "FW: FC-701 Contact Request by Gleam.io");
7
- const result = new EmailForwardParser().read(MY_EMAIL_STRING, "Fwd:");
4
+ MY_EMAIL_STRING = fs.readFileSync("./result+1.txt", "utf-8");
5
+
6
+ const result = new EmailForwardParser().read(MY_EMAIL_STRING, "Fwd: [ACTION REQUIRED] Your Crisp subscription is about to be cancelled");
8
7
 
9
8
  console.log(result.forwarded);
10
9
  console.log(result);
11
- // console.log(result.email.to);
12
- // console.log(result.email.cc);
10
+ console.log(result.email.to);
11
+ console.log(result.email.cc);
12
+ // console.log(result.email.subject);
13
13
 
14
14
  // {
15
15
  // forwarded: false,
package/test.txt DELETED
@@ -1,14 +0,0 @@
1
-
2
- Camille Le Bruchec
3
- Head of Customer Success - Crisp
4
- https://calendly.com/camille-crisp/30min-1
5
- camille@crisp.chat
6
-
7
- > Début du message réexpédié :
8
- >
9
- > De: Camille Le Bruchec <camille.lebruchec@icloud.com>
10
- > Date: 19 avril 2022 à 16:38:24 UTC+2
11
- > À: Camille Le Bruchec <camille@crisp.chat>
12
- >
13
- >
14
- > Hello test 4