-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprepareMessageObject
More file actions
96 lines (74 loc) · 3.48 KB
/
prepareMessageObject
File metadata and controls
96 lines (74 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* Converts inline images and external images into Encoded Data. Removes converted inline images from "attachments collection"
* @param {GmailMessage} messageObject A gmail message
* @return {PreppedMessageObject} result
* @return {GmailMessage} result.messageObject Original gmail message
* @return {String} result.cleanBody new message body
* @return {Attachment} result.cleanAttachments attachments excluding inline images
* @return {String} result.cleanSubject
* @return {String} result.cleanDate
*/
function prepMessageObject(messageObject){
var emailTemplate = messageObject.getBody();
var rawContent = messageObject.getRawContent();
var attachments = messageObject.getAttachments();
var cleanDateString = Utilities.formatDate(messageObject.getDate(), "America/Toronto", "EEE, MMM d, YYYY 'at' h:mm aa");
var hostedImgToReplace = [];
var inlineImgToReplace = [];
var inlineImages = {};
var imgVars = emailTemplate.match(/<img[^>]+>/g); // retrieve all image tags in body.
var regMessageId = new RegExp(messageObject.getId(), "g"); // RegEx to find the message id in strings
if(imgVars != null){
for (var i = 0; i < imgVars.length; i++) {
if (imgVars[i].search(regMessageId) != -1) {
var id = imgVars[i].match(/realattid=([^&]+)&/);
if (id != null) {
var temp = rawContent.split(id[1])[1];
temp = temp.substr(temp.lastIndexOf('Content-Type'));
var imgTitle = temp.match(/name="([^"]+)"/);
if (imgTitle != null) inlineImgToReplace.push([imgTitle[1], imgVars[i], id[1]]);
}
}
else {
hostedImgToReplace.push(["No Title",imgVars[i],"No ID"]);
}
}
}
for (var i = 0; i < inlineImgToReplace.length; i++) {
for (var j = 0; j < attachments.length; j++) {
if(attachments[j].getName() == inlineImgToReplace[i][0]) {
inlineImages[inlineImgToReplace[i][2]] = attachments[j].copyBlob();
var newData = "data:" + inlineImages[inlineImgToReplace[i][2]].getContentType()+';base64,'+ Utilities.base64Encode(inlineImages[inlineImgToReplace[i][2]].getBytes())
attachments.splice(j, 1);
if (newData.substr(0,5) == "data:") {
var newImg = inlineImgToReplace[i][1].replace(/src="[^\"]+\"/, "src=\"" + newData + "\"");
emailTemplate = emailTemplate.replace(inlineImgToReplace[i][1], newImg);
}
}
}
}
for(var i=0;i<hostedImgToReplace.length;i++){
var srcVar = hostedImgToReplace[i][1].match(/(src=\")([^\"]+)(\")/);
if (srcVar != null){
var newData = srcToData(srcVar[2]);
if (newData.substr(0,5) == "data:") {
var newImg = hostedImgToReplace[i][1].replace(/src="[^\"]+\"/, "src=\"" + newData + "\"");
emailTemplate = emailTemplate.replace(hostedImgToReplace[i][1], newImg);
}
}
}
var result = {
messageObject: messageObject,
cleanBody: emailTemplate,
cleanAttachments: attachments,
cleanSubject: messageObject.getSubject() == "" ? "(no subject)" : messageObject.getSubject(),
cleanDateString: cleanDateString,
}
return result;
}
function srcToData(string){
if (string.substr(0,5) == "data:"){return string; }
var img = UrlFetchApp.fetch(string);
var data = img.getBlob().getContentType()+';base64,'+ Utilities.base64Encode(img.getBlob().getBytes());
return "data:"+data;
}