I am working on a JS/JQuery based tool to review and analyze Internet Email Headers. The tools I have created before have relied on the presence of a New Line delimiter. From time to time, I get a customer that has sent in Internet Headers that have been Copy and Pasted enough times that the
or
and no longer read. Often substituted by \s\s
or just \s
. Hence there become no simple, or good way to find each of the lines.
That is exactly what I am trying to do. Given Internet Headers that may or may not contain the proper EOL, how can I capture all the lines or capture the various elements (Received:
, X-Headers:
, Message-ID:
, To:
, From:
, Subject:
, Date:
).
Here is a Fiddle I have been working on: https://jsfiddle.net/Twisty/0n5tmm6L/
Snippet
$("#clean-header").click(function(e) {
e.preventDefault();
if ($("#header-1").val() === "") {
$("#error").html("No headers submitted.");
return false;
}
$("#error").html("");
var textLines = $("#header-1").val().split('
');
if (textLines.length > 1) {
console.log("Found " + textLines.length + " Lines ('\
') in headers.");
return false;
} else {
console.log("No EOL found in Headers. Seeking 'Received:'.");
var s1 = /(Received):\s?from\s(.+?)\s(by.+?);\s(.+?,\s[0-9]{2}\s[a-z]{3}\s[0-9]{4}\s\d{2}:\d{2}:\d{2}.+?)\s{2}/ig;
var match, received = [];
var line = $("#header-1").val();
while (match = s1.exec(line)) {
received.push({
"from": match[2],
"by": match[3],
"stamp": match[4]
});
}
console.log("Found ", received.length, " Received Lines.");
}
});
I'm not afraid to push this to PHP with Pear and use the IMAP library to do this. I was just hoping to do it in the browser without having to involve the server.
In the absence of
, I was hoping to make a few passes with regular expressions like the following:
/(Received):\s?(from.+?)\s(by.+?);\s(.+?,\s[0-9]{2}\s[a-z]{3}\s[0-9]{4}\s\d{2}:\d{2}:\d{2}.+?)\s{2}/ig
/(X-.+?):(.+?)\s\s/ig
/(Reply-To|Return-Path|From):.+?<(.+?)>\s{2}/ig
/(To):.+?<(.+?)>\s{2}/ig
I never know if what the EOL will be, if anything. I have also hunted to see if there are any good add-ons or libraries that already do this. So far, not seen any. Suggestions greatly welcome. Alternatives also welcome.