Skip to content

Commit 7959f38

Browse files
authored
feat: Add example of sending an email with an inline image. (googleworkspace#203)
* Add example of sending an email with an inline image. * Apply review suggestions 1. Add Apache header 2. var -> const 3. Refactor getImage() to getImageBinary() to keep the relevant parts together. Also: 4. Update the emoji used to a smaller one (to make it more readable) and update the link (the old one broke). 5. Refactor the image name to a single const so it's easy to see that it needs to be consistent across several places. Note: The brackets around `imageName` are required because it's a [computed property name](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#computed_property_names). * Add year to copyright.
1 parent 21c7802 commit 7959f38

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

gmail/inlineimage/inlineimage.gs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
/**
3+
* Copyright 2022 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
function sendEmailToMyself() {
19+
// You can use this method to test the welcome email.
20+
sendEmailWithInlineImage(Session.getActiveUser().getEmail());
21+
}
22+
23+
function sendEmailWithInlineImage(toAddress) {
24+
const options = {};
25+
const imageName = 'cat_emoji';
26+
// The URL "cid:cat_emoji" means that the inline attachment named "cat_emoji" would be used.
27+
options['htmlBody'] = 'Welcome! <img src="cid:' + imageName + '" alt="Cat Emoji" />';
28+
options['inlineImages'] = {[imageName]: Utilities.newBlob(getImageBinary(), 'image/png', imageName)};
29+
GmailApp.sendEmail(toAddress, 'Welcome!', 'Welcome!', options);
30+
}
31+
32+
function getImageBinary() {
33+
// Cat Face Emoji from https://github.com/googlefonts/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