From e3b6aea9304afbfd9da9ed0c06699bc27a72352e Mon Sep 17 00:00:00 2001 From: tyochelson-lwhs <78885812+tyochelson-lwhs@users.noreply.github.com> Date: Wed, 10 Feb 2021 17:20:43 -0800 Subject: [PATCH 1/2] Process Draft Inline Images to send properly Per Issue #163 -- handling of Inline Images varies between GmailDraft and GmailMessage classes; code changes convert attachments to inlineImages and update tags in HTML Body text to conform to updated usage. --- mail-merge/src/Code.js | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/mail-merge/src/Code.js b/mail-merge/src/Code.js index 1087a7aa..8433490a 100644 --- a/mail-merge/src/Code.js +++ b/mail-merge/src/Code.js @@ -95,7 +95,8 @@ function sendEmails(subjectLine, sheet=SpreadsheetApp.getActiveSheet()) { // name: 'name of the sender', // replyTo: 'a.reply@email.com', // noReply: true, // if the email should be sent from a generic no-reply email address (not available to gmail.com users) - attachments: emailTemplate.attachments + attachments: emailTemplate.attachments, + inlineImages: emailTemplate.inlineImages }); // modify cell to record email sent date out.push([new Date()]); @@ -115,6 +116,9 @@ function sendEmails(subjectLine, sheet=SpreadsheetApp.getActiveSheet()) { * Get a Gmail draft message by matching the subject line. * @param {string} subject_line to search for draft message * @return {object} containing the subject, plain and html message body and attachments + * + * Incorporates code to correctly parse and insert inline images + * @see https://stackoverflow.com/a/49621562 */ function getGmailTemplateFromDrafts_(subject_line){ try { @@ -126,8 +130,39 @@ function sendEmails(subjectLine, sheet=SpreadsheetApp.getActiveSheet()) { const msg = draft.getMessage(); // getting attachments so they can be included in the merge const attachments = msg.getAttachments(); - return {message: {subject: subject_line, text: msg.getPlainBody(), html:msg.getBody()}, - attachments: attachments}; + // get HTML body text + var html_Body = msg.getBody(); + // Configure a RegExp for identifying draft Image Tags that indicate "inline" images + const regDraftImgTag = new RegExp('img data-surl="cid:', "g"); + + if (html_Body.match(regDraftImgTag) != null) { + var inlineImages = {}; + var imgVars = html_Body.match(/]+>/g); + var imgToReplace = []; + if(imgVars != null){ + for (var i = 0; i < imgVars.length; i++) { + if (imgVars[i].search(regDraftImgTag) != -1) { + var id = imgVars[i].match(/src="cid:([^"]+)"/); + if (id != null) { + var imgTitle = imgVars[i].match(/alt="([^"]+)"/); + if (imgTitle != null) imgToReplace.push([imgTitle[1], imgVars[i], id[1]]); + } + } + } + } + for (var i = 0; i < imgToReplace.length; i++) { + for (var j = 0; j < attachments.length; j++) { + if(attachments[j].getName() == imgToReplace[i][0]) { + inlineImages[imgToReplace[i][2]] = attachments[j].copyBlob(); + attachments.splice(j, 1); + var newImg = imgToReplace[i][1].replace(/ data-surl="[^"]+"/, ""); + html_Body = html_Body.replace(imgToReplace[i][1], newImg); + } + } + } + } + return {message: {subject: subject_line, text: msg.getPlainBody(), html: html_Body }, + attachments: attachments, inlineImages: inlineImages}; } catch(e) { throw new Error("Oops - can't find Gmail draft"); } From e802ed83628df34403d85a81a4ec070e7b9e5b1d Mon Sep 17 00:00:00 2001 From: tyochelson-lwhs Date: Thu, 11 Feb 2021 19:14:15 -0800 Subject: [PATCH 2/2] Process Draft Inline Images to send properly Resolves Issue #163 -- handling of Inline Images varies between GmailDraft and GmailMessage classes; code changes convert attachments to inlineImages and update tags in HTML Body text to conform to updated usage. Re-committing to associate my email address for CLA-foo. --- mail-merge/src/Code.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mail-merge/src/Code.js b/mail-merge/src/Code.js index 8433490a..4d25e806 100644 --- a/mail-merge/src/Code.js +++ b/mail-merge/src/Code.js @@ -217,4 +217,4 @@ function sendEmails(subjectLine, sheet=SpreadsheetApp.getActiveSheet()) { .replace(/[\r]/g, '\\r') .replace(/[\t]/g, '\\t'); }; -} +}