Skip to content
This repository was archived by the owner on Dec 7, 2023. It is now read-only.

Commit aaa2082

Browse files
authored
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 #65 (comment) * update to head * Updated to head * Fix for null error when sending emails without variable tags Fix for issue #98 on gsuitedevs branch #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 #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 #163 * Nit fix for mail merge #163 * Revert "Inline image handling for mail merge" This reverts commit 363060c. * Fix for mail merge inline image handling #163 Added handling of inline images inserted in the Gmail draft via image upload
1 parent 1fc1b1c commit aaa2082

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

mail-merge/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Create and distribute visually rich mail merges with Gmail and Goog
44
labels: Sheets, Gmail
55
material_icon: merge_type
66
create_time: 2019-09-13
7-
update_time: 2020-04-29
7+
update_time: 2021-02-14
88
---
99

1010
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/).

mail-merge/src/Code.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
/**
2020
* Change these to match the column names you are using for email
21-
* recepient addresses and email sent column.
21+
* recipient addresses and email sent column.
2222
*/
2323
const RECIPIENT_COL = "Recipient";
2424
const EMAIL_SENT_COL = "Email Sent";
@@ -95,7 +95,8 @@ function sendEmails(subjectLine, sheet=SpreadsheetApp.getActiveSheet()) {
9595
// name: 'name of the sender',
9696
// replyTo: 'a.reply@email.com',
9797
// noReply: true, // if the email should be sent from a generic no-reply email address (not available to gmail.com users)
98-
attachments: emailTemplate.attachments
98+
attachments: emailTemplate.attachments,
99+
inlineImages: emailTemplate.inlineImages
99100
});
100101
// modify cell to record email sent date
101102
out.push([new Date()]);
@@ -124,10 +125,29 @@ function sendEmails(subjectLine, sheet=SpreadsheetApp.getActiveSheet()) {
124125
const draft = drafts.filter(subjectFilter_(subject_line))[0];
125126
// get the message object
126127
const msg = draft.getMessage();
127-
// getting attachments so they can be included in the merge
128-
const attachments = msg.getAttachments();
129-
return {message: {subject: subject_line, text: msg.getPlainBody(), html:msg.getBody()},
130-
attachments: attachments};
128+
129+
// Handling inline images and attachments so they can be included in the merge
130+
// Based on https://stackoverflow.com/a/65813881/1027723
131+
// Get all attachments and inline image attachments
132+
const allInlineImages = draft.getMessage().getAttachments({includeInlineImages: true,includeAttachments:false});
133+
const attachments = draft.getMessage().getAttachments({includeInlineImages: false});
134+
const htmlBody = msg.getBody();
135+
136+
// Create an inline image object with the image name as key
137+
// (can't rely on image index as array based on insert order)
138+
const img_obj = allInlineImages.reduce((obj, i) => (obj[i.getName()] = i, obj) ,{});
139+
140+
//Regexp to search for all img string positions with cid
141+
const imgexp = RegExp('<img.*?src="cid:(.*?)".*?alt="(.*?)"[^\>]+>', 'g');
142+
const matches = [...htmlBody.matchAll(imgexp)];
143+
144+
//Initiate the allInlineImages object
145+
const inlineImagesObj = {};
146+
// built an inlineImagesObj from inline image matches
147+
matches.forEach(match => inlineImagesObj[match[1]] = img_obj[match[2]]);
148+
149+
return {message: {subject: subject_line, text: msg.getPlainBody(), html:htmlBody},
150+
attachments: attachments, inlineImages: inlineImagesObj };
131151
} catch(e) {
132152
throw new Error("Oops - can't find Gmail draft");
133153
}
@@ -160,7 +180,7 @@ function sendEmails(subjectLine, sheet=SpreadsheetApp.getActiveSheet()) {
160180

161181
// token replacement
162182
template_string = template_string.replace(/{{[^{}]+}}/g, key => {
163-
return escapeData_(data[key.replace(/[{}]+/g, "")]) || "";
183+
return escapeData_(data[key.replace(/[{}]+/g, "")] || "");
164184
});
165185
return JSON.parse(template_string);
166186
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy