|
18 | 18 |
|
19 | 19 | /**
|
20 | 20 | * 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. |
22 | 22 | */
|
23 | 23 | const RECIPIENT_COL = "Recipient";
|
24 | 24 | const EMAIL_SENT_COL = "Email Sent";
|
@@ -95,7 +95,8 @@ function sendEmails(subjectLine, sheet=SpreadsheetApp.getActiveSheet()) {
|
95 | 95 | // name: 'name of the sender',
|
96 | 96 |
|
97 | 97 | // 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 |
99 | 100 | });
|
100 | 101 | // modify cell to record email sent date
|
101 | 102 | out.push([new Date()]);
|
@@ -124,10 +125,29 @@ function sendEmails(subjectLine, sheet=SpreadsheetApp.getActiveSheet()) {
|
124 | 125 | const draft = drafts.filter(subjectFilter_(subject_line))[0];
|
125 | 126 | // get the message object
|
126 | 127 | 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 }; |
131 | 151 | } catch(e) {
|
132 | 152 | throw new Error("Oops - can't find Gmail draft");
|
133 | 153 | }
|
@@ -160,7 +180,7 @@ function sendEmails(subjectLine, sheet=SpreadsheetApp.getActiveSheet()) {
|
160 | 180 |
|
161 | 181 | // token replacement
|
162 | 182 | template_string = template_string.replace(/{{[^{}]+}}/g, key => {
|
163 |
| - return escapeData_(data[key.replace(/[{}]+/g, "")]) || ""; |
| 183 | + return escapeData_(data[key.replace(/[{}]+/g, "")] || ""); |
164 | 184 | });
|
165 | 185 | return JSON.parse(template_string);
|
166 | 186 | }
|
|
0 commit comments