Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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