Skip to content

Instantly share code, notes, and snippets.

@davecra
Created May 24, 2019 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davecra/0ee67e489b125fbf2c0a5aa32553ea9e to your computer and use it in GitHub Desktop.
Save davecra/0ee67e489b125fbf2c0a5aa32553ea9e to your computer and use it in GitHub Desktop.
Simple client-side MIME parsing object
/**
* Simple MIME Object for parsing basic MIME results:
* - Headers (parts)
* - HTML (full)
* - Attachments
* - Embedded Image Data
*/
function simpleMIMEClass() {
/** @type {string} */
var fullHeader = "";
/** @type {string} */
var html = "";
/** @type {MIMEImageData[]} */
var images = new Array();
/** @type {string} */
var text = "";
/** @type {string} */
var mimeData = "";
/**
* Internal MIME Image Class
*/
function MIMEImageData(){
this.ID = "";
this.Data = "";
}
/** @type {string} */
this.FullHeader = function() { return fullHeader };
/** @type {string} */
this.HTML = function() { return html; }
/** @type {string} */
this.Text = function() { return text; }
/** @type {string} */
this.GetImageData = function(id) {
for(var i=0;i<images.length;i++) {
if(images[i].ID == id) {
return images[i].Data;
}
}
// if here - nothing
return "";
}
/**
* Parses the MIME data into the object
* @param {@type {string} value} - MIME data from getMailItemMimeContent()
*/
this.Parse = function(value) {
mimeData = value;
text = atob(mimeData);
var parts = text.split("\r\n\r\n");
fullHeader = parts[0];
for(var partIndex=0;partIndex<parts.length;partIndex++) {
if(parts[partIndex].includes("Content-Type: text/html;", 0) > 0) {
html = parts[partIndex+1];
// must remove the =3D which is an incomplete escape code for "="
// which gets into Outlook MIME somehow - now sure
// also removing line breaks which are = at the very end
// followed by carriage return and new line
html = html.replace(/=3D/g,'=').replace(/=\r\n/g,"");
}
if(parts[partIndex].includes("Content-Type: image/", 0) > 0) {
var imgTag = parts[partIndex].split("\r\n");
var imgData = parts[partIndex+1];
var imgID = "";
for(var tagIndex=0;tagIndex<imgTag.length;tagIndex++) {
if(imgTag[tagIndex].includes("Content-ID: ") > 0) {
imgID = "cid:" + imgTag[tagIndex].split(": ")[1].replace("<","").replace(">","");
}
}
var img = new MIMEImageData();
img.Data = imgData;
img.ID = imgID;
images.push(img);
}
}
// done
return this;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment