From aaa2082020a7363860940b85dc42113c13d4d421 Mon Sep 17 00:00:00 2001 From: mhawksey Date: Wed, 17 Feb 2021 20:26:29 +0000 Subject: [PATCH] Patch to mail-merge solution which address the issue on inline images from the Gmail draft (#173) * Initial commit of Mail Merge solution Create and distribute visual rich mail merges with Gmail and Google Sheets * Minor edits to readme text Edits based on comments in https://github.com/gsuitedevs/solutions/pull/65#issue-317359815 * update to head * Updated to head * Fix for null error when sending emails without variable tags Fix for issue #98 on gsuitedevs branch https://github.com/gsuitedevs/solutions/issues/98 * Refactoring and V8 update Includes ability for using formatted Google Sheets cell values for currencies, dates and more as well as detecting/ignoring filtered hidden rows. Code also updated to highlight Gmail `.sendMail()` options * Updated mail merge solution moving Advanced Sheets Service and moving to V8 Removed dependency on Advanced Sheets Service * Mail merge fix to escape cell data Fix for https://github.com/gsuitedevs/solutions/issues/127 to escape cell data to make JSON safe * Fixed EOF https://github.com/gsuitedevs/solutions/pull/130/files/a8d745d949b33484dd6a33da4c0dfcfb1202cfdf#r431210317 * Inline image handling for mail merge Fix for the inline image issue https://github.com/googleworkspace/solutions/issues/163 * Nit fix for mail merge #163 * Revert "Inline image handling for mail merge" This reverts commit 363060c5985d2a25602d078d0c50f4662e72ccb8. * Fix for mail merge inline image handling https://github.com/googleworkspace/solutions/issues/163 Added handling of inline images inserted in the Gmail draft via image upload --- mail-merge/README.md | 2 +- mail-merge/src/Code.js | 34 +++++++++++++++++++++++++++------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/mail-merge/README.md b/mail-merge/README.md index f8cbc61e..e0834e36 100644 --- a/mail-merge/README.md +++ b/mail-merge/README.md @@ -4,7 +4,7 @@ description: Create and distribute visually rich mail merges with Gmail and Goog labels: Sheets, Gmail material_icon: merge_type create_time: 2019-09-13 -update_time: 2020-04-29 +update_time: 2021-02-14 --- Contributed by Martin Hawksey, follow me on Twitter [@mhawksey](https://twitter.com/mhawksey) or [read my Google Apps Script related blog posts](https://mashe.hawksey.info/category/google-apps-script/). diff --git a/mail-merge/src/Code.js b/mail-merge/src/Code.js index 1087a7aa..1bd2d104 100644 --- a/mail-merge/src/Code.js +++ b/mail-merge/src/Code.js @@ -18,7 +18,7 @@ /** * Change these to match the column names you are using for email - * recepient addresses and email sent column. + * recipient addresses and email sent column. */ const RECIPIENT_COL = "Recipient"; const EMAIL_SENT_COL = "Email Sent"; @@ -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()]); @@ -124,10 +125,29 @@ function sendEmails(subjectLine, sheet=SpreadsheetApp.getActiveSheet()) { const draft = drafts.filter(subjectFilter_(subject_line))[0]; // get the message object 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}; + + // Handling inline images and attachments so they can be included in the merge + // Based on https://stackoverflow.com/a/65813881/1027723 + // Get all attachments and inline image attachments + const allInlineImages = draft.getMessage().getAttachments({includeInlineImages: true,includeAttachments:false}); + const attachments = draft.getMessage().getAttachments({includeInlineImages: false}); + const htmlBody = msg.getBody(); + + // Create an inline image object with the image name as key + // (can't rely on image index as array based on insert order) + const img_obj = allInlineImages.reduce((obj, i) => (obj[i.getName()] = i, obj) ,{}); + + //Regexp to search for all img string positions with cid + const imgexp = RegExp(']+>', 'g'); + const matches = [...htmlBody.matchAll(imgexp)]; + + //Initiate the allInlineImages object + const inlineImagesObj = {}; + // built an inlineImagesObj from inline image matches + matches.forEach(match => inlineImagesObj[match[1]] = img_obj[match[2]]); + + return {message: {subject: subject_line, text: msg.getPlainBody(), html:htmlBody}, + attachments: attachments, inlineImages: inlineImagesObj }; } catch(e) { throw new Error("Oops - can't find Gmail draft"); } @@ -160,7 +180,7 @@ function sendEmails(subjectLine, sheet=SpreadsheetApp.getActiveSheet()) { // token replacement template_string = template_string.replace(/{{[^{}]+}}/g, key => { - return escapeData_(data[key.replace(/[{}]+/g, "")]) || ""; + return escapeData_(data[key.replace(/[{}]+/g, "")] || ""); }); return JSON.parse(template_string); }