Skip to content

Commit 1463b22

Browse files
authored
Merge pull request #7 from omkar9999/master
CC and Bcc Email Addresses & Split Long Running Regex & renderDataUri
2 parents ec2c135 + dac29db commit 1463b22

File tree

1 file changed

+76
-17
lines changed

1 file changed

+76
-17
lines changed

Diff for: GmailUtils.gs

+76-17
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ function messageToHtml(messages, opts) {
196196
date = formatDate(message),
197197
from = formatEmails_(message.getFrom()),
198198
to = formatEmails_(message.getTo()),
199+
cc = formatEmails_(message.getCc()),
200+
bcc = formatEmails_(message.getBcc()),
199201
body = message.getBody();
200202

201203
if (opts.includeHeader) {
@@ -208,9 +210,16 @@ function messageToHtml(messages, opts) {
208210
'<dt>From:</dt>' + avatar + ' <dd class="strong">' + from + '</dd>\n' +
209211
'<dt>Subject:</dt> <dd>' + subject + '</dd>\n' +
210212
'<dt>Date:</dt> <dd>' + date + '</dd>\n' +
211-
'<dt>To:</dt> <dd>' + to + '</dd>\n' +
212-
'</dl>\n';
213+
'<dt>To:</dt> <dd>' + to + '</dd>\n';
213214
}
215+
// Appending cc and bcc if they exist
216+
if(isRealValue(cc)){
217+
html += '<dt>cc:</dt> <dd>' + cc + '</dd>\n';
218+
}
219+
if(isRealValue(bcc)){
220+
html += '<dt>bcc:</dt> <dd>' + bcc + '</dd>\n';
221+
}
222+
html += '</dl>\n';
214223
if (opts.embedRemoteImages) {
215224
body = embedHtmlImages_(body);
216225
}
@@ -284,25 +293,55 @@ function emailGetAvatar(email) {
284293
*/
285294
function embedHtmlImages_(html) {
286295
// process all img tags
287-
html = html.replace(/(<img[^>]+src=)(["'])((?:(?!\2)[^\\]|\\.)*)\2/gi, function(m, tag, q, src) {
288-
// Logger.log('Processing image src: ' + src);
289-
return tag + q + (renderDataUri_(src) || src) + q;
290-
});
296+
html = processImageTags(html);
291297
// process all style attributes
292-
html = html.replace(/(<[^>]+style=)(["'])((?:(?!\2)[^\\]|\\.)*)\2/gi, function(m, tag, q, style) {
293-
style = style.replace(/url\((\\?["']?)([^\)]*)\1\)/gi, function(m, q, url) {
294-
return 'url(' + q + (renderDataUri_(url) || url) + q + ')';
295-
});
296-
return tag + q + style + q;
297-
});
298+
html = processStyleAttributes(html);
298299
// process all style tags
299-
html = html.replace(/(<style[^>]*>)(.*?)(?:<\/style>)/gi, function(m, tag, style, end) {
300+
html = processStyleTags(html);
301+
return html;
302+
}
303+
304+
/**
305+
* Download and embed all img tags
306+
*
307+
* @param {string} html
308+
* @return {string} Html with embedded images
309+
*/
310+
function processImageTags(html){
311+
return html.replace(/(<img[^>]+src=)(["'])((?:(?!\2)[^\\]|\\.)*)\2/gi, function(m, tag, q, src) {
312+
// Logger.log('Processing image src: ' + src);
313+
return tag + q + (renderDataUri_(src) || src) + q;
314+
});
315+
}
316+
317+
/**
318+
* Download and embed all HTML Style Attributes
319+
*
320+
* @param {string} html
321+
* @return {string} Html with embedded style attributes
322+
*/
323+
function processStyleAttributes(html){
324+
return html.replace(/(<[^>]+style=)(["'])((?:(?!\2)[^\\]|\\.)*)\2/gi, function(m, tag, q, style) {
325+
style = style.replace(/url\((\\?["']?)([^\)]*)\1\)/gi, function(m, q, url) {
326+
return 'url(' + q + (renderDataUri_(url) || url) + q + ')';
327+
});
328+
return tag + q + style + q;
329+
});
330+
}
331+
332+
/**
333+
* Download and embed all HTML Style Tags
334+
*
335+
* @param {string} html
336+
* @return {string} Html with embedded style tags
337+
*/
338+
function processStyleTags(html){
339+
return html.replace(/(<style[^>]*>)(.*?)(?:<\/style>)/gi, function(m, tag, style, end) {
300340
style = style.replace(/url\((["']?)([^\)]*)\1\)/gi, function(m, q, url) {
301341
return 'url(' + q + (renderDataUri_(url) || url) + q + ')';
302342
});
303343
return tag + style + end;
304344
});
305-
return html;
306345
}
307346

308347
/**
@@ -344,9 +383,19 @@ function embedInlineImages_(html, raw) {
344383
}).filter(function(i){return i});
345384

346385
// process all img tags which reference "attachments"
347-
return html.replace(/(<img[^>]+src=)(["'])(\?view=att(?:(?!\2)[^\\]|\\.)*)\2/gi, function(m, tag, q, src) {
348-
return tag + q + (renderDataUri_(images.shift()) || src) + q;
349-
});
386+
return processImgAttachments(html);
387+
}
388+
389+
/**
390+
* Download and embed all HTML Inline Image Attachments
391+
*
392+
* @param {string} html
393+
* @return {string} Html with inline image attachments
394+
*/
395+
function processImgAttachments(html){
396+
return html.replace(/(<img[^>]+src=)(["'])(\?view=att(?:(?!\2)[^\\]|\\.)*)\2/gi, function(m, tag, q, src) {
397+
return tag + q + (renderDataUri_(images.shift()) || src) + q;
398+
});
350399
}
351400

352401
/**
@@ -455,6 +504,16 @@ function localTimezone_() {
455504
return timezone.length ? timezone[1] : 'GMT';
456505
}
457506

507+
/**
508+
* Check if value is not null or undefined
509+
*
510+
* @param {Object} obj
511+
* @return {boolean} true if object is not null or undefined
512+
*/
513+
function isRealValue(obj) {
514+
return obj && obj !== 'null' && obj !== 'undefined';
515+
}
516+
458517
/**
459518
* Create an MD5 hash of a string and return the reult as hexadecimal.
460519
*

0 commit comments

Comments
 (0)