Gmail addon createdraftreply() replying to yourself - gmail-addons

I am referencing the official guide
here
to create a reply draft.
var composeAction = CardService.newAction()
.setFunctionName('createReplyDraft');
var composeButton = CardService.newTextButton()
.setText('Compose Reply')
.setComposeAction(composeAction, CardService.ComposedEmailType.REPLY_AS_DRAFT);
// ...
/**
* Creates a draft email (with an attachment and inline image)
* as a reply to an existing message.
* #param {Object} e data passed by the compose action.
* #return {ComposeActionResponse}
*/
function createReplyDraft(e) {
// Activate temporary Gmail add-on scopes, in this case to allow
// a reply to be drafted.
var accessToken = e.messageMetadata.accessToken;
GmailApp.setCurrentMessageAccessToken(accessToken);
// Creates a draft reply.
var messageId = e.messageMetadata.messageId;
var message = GmailApp.getMessageById(messageId);
var draft = message.createDraftReply('',
{
htmlBody: "Kitten! <img src='cid:kitten'/>",
attachments: [
UrlFetchApp.fetch('https://example.com/images/myDog.jpg')
.getBlob()
],
inlineImages: {
"kitten": UrlFetchApp.fetch('https://example.com/images/myKitten.jpg')
.getBlob()
}
}
);
// Return a built draft response. This causes Gmail to present a
// compose window to the user, pre-filled with the content specified
// above.
return CardService.newComposeActionResponseBuilder()
.setGmailDraft(draft).build();
}
It works fine when the last message in the thread is from the other person,
but when the last message is from yourself,
the recipient address of the new draft is your address, not the other person.
1.
(Last message) Other person → Me
createDraftReply()
(Draft) Me → Other person
2.
(Last message) Me → Other person
createDraftReply()
(Draft) Me → Me
How can I create a reply draft with the right recipient?

Before creating a reply draft check whether the TO address is self or not.
If TO is self, create reply all draft instead of reply draft
Else create reply draft.
if(message.getTo() === Session.getEffectiveUser().getEmail())
This is how you can check.
OR
There is one more way, you can iterate over all emails and pick the latest mail from the outsider and create a reply draft for it.
var mssgs = message.getThread().getMessages();

Related

GmailApp.sendEmail: is it possible to schedule send an email?

I try to create my mail merge with this example.
However, I don't want to send all emails immediately after using the script - I'd like to create scheduled messages with dates specified in a column (Scheduled time).
The script from the example:
// To learn how to use this script, refer to the documentation:
// https://developers.google.com/apps-script/samples/automations/mail-merge
/*
Copyright 2022 Martin Hawksey
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* #OnlyCurrentDoc
*/
/**
* Change these to match the column names you are using for email
* recipient addresses and email sent column.
*/
const RECIPIENT_COL = "Recipient";
const EMAIL_SENT_COL = "Email Sent";
/**
* Creates the menu item "Mail Merge" for user to run scripts on drop-down.
*/
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('Mail Merge')
.addItem('Send Emails', 'sendEmails')
.addToUi();
}
/**
* Sends emails from sheet data.
* #param {string} subjectLine (optional) for the email draft message
* #param {Sheet} sheet to read data from
*/
function sendEmails(subjectLine, sheet=SpreadsheetApp.getActiveSheet()) {
// option to skip browser prompt if you want to use this code in other projects
if (!subjectLine){
subjectLine = Browser.inputBox("Mail Merge",
"Type or copy/paste the subject line of the Gmail " +
"draft message you would like to mail merge with:",
Browser.Buttons.OK_CANCEL);
if (subjectLine === "cancel" || subjectLine == ""){
// If no subject line, finishes up
return;
}
}
// Gets the draft Gmail message to use as a template
const emailTemplate = getGmailTemplateFromDrafts_(subjectLine);
// Gets the data from the passed sheet
const dataRange = sheet.getDataRange();
// Fetches displayed values for each row in the Range HT Andrew Roberts
// https://mashe.hawksey.info/2020/04/a-bulk-email-mail-merge-with-gmail-and-google-sheets-solution-evolution-using-v8/#comment-187490
// #see https://developers.google.com/apps-script/reference/spreadsheet/range#getdisplayvalues
const data = dataRange.getDisplayValues();
// Assumes row 1 contains our column headings
const heads = data.shift();
// Gets the index of the column named 'Email Status' (Assumes header names are unique)
// #see http://ramblings.mcpher.com/Home/excelquirks/gooscript/arrayfunctions
const emailSentColIdx = heads.indexOf(EMAIL_SENT_COL);
// Converts 2d array into an object array
// See https://stackoverflow.com/a/22917499/1027723
// For a pretty version, see https://mashe.hawksey.info/?p=17869/#comment-184945
const obj = data.map(r => (heads.reduce((o, k, i) => (o[k] = r[i] || '', o), {})));
// Creates an array to record sent emails
const out = [];
// Loops through all the rows of data
obj.forEach(function(row, rowIdx){
// Only sends emails if email_sent cell is blank and not hidden by a filter
if (row[EMAIL_SENT_COL] == ''){
try {
const msgObj = fillInTemplateFromObject_(emailTemplate.message, row);
// See https://developers.google.com/apps-script/reference/gmail/gmail-app#sendEmail(String,String,String,Object)
// If you need to send emails with unicode/emoji characters change GmailApp for MailApp
// Uncomment advanced parameters as needed (see docs for limitations)
GmailApp.sendEmail(row[RECIPIENT_COL], msgObj.subject, msgObj.text, {
htmlBody: msgObj.html,
// bcc: 'a.bbc#email.com',
// cc: 'a.cc#email.com',
from: 'sender#gmail.com',
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,
inlineImages: emailTemplate.inlineImages
});
// Edits cell to record email sent date
out.push([new Date()]);
} catch(e) {
// modify cell to record error
out.push([e.message]);
}
} else {
out.push([row[EMAIL_SENT_COL]]);
}
});
// Updates the sheet with new data
sheet.getRange(2, emailSentColIdx+1, out.length).setValues(out);
/**
* 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
*/
function getGmailTemplateFromDrafts_(subject_line){
try {
// get drafts
const drafts = GmailApp.getDrafts();
// filter the drafts that match subject line
const draft = drafts.filter(subjectFilter_(subject_line))[0];
// get the message object
const msg = draft.getMessage();
// Handles inline images and attachments so they can be included in the merge
// Based on https://stackoverflow.com/a/65813881/1027723
// Gets 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();
// Creates 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 searches for all img string positions with cid
const imgexp = RegExp('<img.*?src="cid:(.*?)".*?alt="(.*?)"[^\>]+>', 'g');
const matches = [...htmlBody.matchAll(imgexp)];
//Initiates 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");
}
/**
* Filter draft objects with the matching subject linemessage by matching the subject line.
* #param {string} subject_line to search for draft message
* #return {object} GmailDraft object
*/
function subjectFilter_(subject_line){
return function(element) {
if (element.getMessage().getSubject() === subject_line) {
return element;
}
}
}
}
/**
* Fill template string with data object
* #see https://stackoverflow.com/a/378000/1027723
* #param {string} template string containing {{}} markers which are replaced with data
* #param {object} data object used to replace {{}} markers
* #return {object} message replaced with data
*/
function fillInTemplateFromObject_(template, data) {
// We have two templates one for plain text and the html body
// Stringifing the object means we can do a global replace
let template_string = JSON.stringify(template);
// Token replacement
template_string = template_string.replace(/{{[^{}]+}}/g, key => {
return escapeData_(data[key.replace(/[{}]+/g, "")] || "");
});
return JSON.parse(template_string);
}
/**
* Escape cell data to make JSON safe
* #see https://stackoverflow.com/a/9204218/1027723
* #param {string} str to escape JSON special characters from
* #return {string} escaped string
*/
function escapeData_(str) {
return str
.replace(/[\\]/g, '\\\\')
.replace(/[\"]/g, '\\\"')
.replace(/[\/]/g, '\\/')
.replace(/[\b]/g, '\\b')
.replace(/[\f]/g, '\\f')
.replace(/[\n]/g, '\\n')
.replace(/[\r]/g, '\\r')
.replace(/[\t]/g, '\\t');
};
}
Is it possible to set sending date with the GmaillApp.sendEmail function? I wasn't able to find a way in the documentation.
If not, is there another way to achieve this with Google Sheets? Any suggestion will be appreciated.
Scheduling with Google Apps Script is possible using Triggers.
Click Triggers on left menu.
Then "Add Trigger" button and select the function and time to run automatically.
To run your function with an specific date from your cell, see this other question: Run trigger at specific date and time
Documentation: https://developers.google.com/apps-script/reference/script/clock-trigger-builder
Hope it helps.
Yes, almost anything is possible with AppScript.
I suggest you read the sheet into an array and then create a loop that compares the current time to the scheduled time. If they are equal (may not be able to get it to the exact second, minutes likely would be better), sends the email and removes the element from the array. The loop continues until the array is empty.

How to append texts/html to the current created draft in Gmail Addons in the real time

I have this scenario once the user clicks on the LINK text button:
If the draft box is not opened, create a new draft reply and append the link.
If the draft box is opened, just append the needed text to the written texts by the user.
I could be able to create a new draft with the needed text, but I've some of difficulties to figure out the second scenario! I could be able to update the created draft, but in the background and the user can't see those changes until he goes to the draft folder and opens the draft message!
//Build the link text button
var appendLink = CardService.newTextButton().setText("Link").setComposeAction(CardService.newAction().setFunctionName("appendTemplateLinkBtnAction").setParameters({templateLink:theCustomizedTemplateLinkText}), CardService.ComposedEmailType.REPLY_AS_DRAFT);
//-----------------------------------------------------------------//
//Append the customized template link button action
function appendTemplateLinkBtnAction(e){
//Get the access token
var theAccessToken = e.messageMetadata.accessToken;
GmailApp.setCurrentMessageAccessToken(theAccessToken);
var theTemplateLink = e.parameters.templateLink;
var theMessageId = e.messageMetadata.messageId;
var theMessage = GmailApp.getMessageById(theMessageId);
var theDraft = theMessage.createDraftReply(theTemplateLink);
//Build the draft
return CardService.newComposeActionResponseBuilder().setGmailDraft(theDraft).build();
}
For the 1st part, you can provide options to your draft.
var draft = GmailApp.createDraft(
email,
subject,
'',
{
htmlBody: errorText + body,
attachments: []
}
)
return CardService.newComposeActionResponseBuilder()
.setGmailDraft(draft).build();

Handling persistent user-specific values in Gmail Add-ons

I have created simple Gmail addon, now I'm struggling with below strategies.
After installing the addon, when first inbox is opened, we ask basic info input from users, when he opens second mail we won't ask basic info details again.
How I can handle this?
I have tried property service but no luck.
Update
var MAX_THREADS = 5;
/**
* Returns the array of cards that should be rendered for the current
* e-mail thread. The name of this function is specified in the
* manifest 'onTriggerFunction' field, indicating that this function
* runs every time the add-on is started.
*
* #param {Object} e data provided by the Gmail UI.
* #returns {Card[]}
*/
function buildAddOn(e) {
// Activate temporary Gmail add-on scopes.
//Logger.log('E', Session.getActiveUser());
var accessToken = e.messageMetadata.accessToken;
GmailApp.setCurrentMessageAccessToken(accessToken);
var userProperties = PropertiesService.getUserProperties();
var Token = userProperties.getProperty('Token');
Logger.log('Token value:',typeof Token);
if(Token != null ){
var messageId = e.messageMetadata.messageId;
var senderData = extractSenderData(messageId);
var cards = [];
// Build a card for each recent thread from this email's sender.
if (senderData.recents.length > 0) {
senderData.recents.forEach(function(threadData) {
cards.push(buildRecentThreadCard(senderData.email, threadData));
});
} else {
// Present a blank card if there are no recent threads from
// this sender.
cards.push(CardService.newCardBuilder()
.setHeader(CardService.newCardHeader()
.setTitle('No recent threads from this sender')).build());
}
return cards;
}
else{
var cards = []
var login_card = build_login_card()
cards.push(login_card);
return cards;
}
}
/**
* This function builds a set of data about this sender's presence in your
* inbox.
*
* #param {String} messageId The message ID of the open message.
* #return {Object} a collection of sender information to display in cards.
*/
function extractSenderData(messageId) {
// Use the Gmail service to access information about this message.
var mail = GmailApp.getMessageById(messageId);
var threadId = mail.getThread().getId();
var senderEmail = extractEmailAddress(mail.getFrom());
var recentThreads = GmailApp.search('from:' + senderEmail);
var recents = [];
// Retrieve information about up to 5 recent threads from the same sender.
recentThreads.slice(0,MAX_THREADS).forEach(function(thread) {
if (thread.getId() != threadId && ! thread.isInChats()) {
recents.push({
'subject': thread.getFirstMessageSubject(),
'count': thread.getMessageCount(),
'link': 'https://mail.google.com/mail/u/0/#inbox/' + thread.getId(),
'lastDate': thread.getLastMessageDate().toDateString()
});
}
});
var senderData = {
"email": senderEmail,
'recents': recents
};
return senderData;
}
/**
* Given the result of GmailMessage.getFrom(), extract only the email address.
* getFrom() can return just the email address or a string in the form
* "Name <myemail#domain>".
*
* #param {String} sender The results returned from getFrom().
* #return {String} Only the email address.
*/
function extractEmailAddress(sender) {
var regex = /\<([^\#]+\#[^\>]+)\>/;
var email = sender; // Default to using the whole string.
var match = regex.exec(sender);
if (match) {
email = match[1];
}
return email;
}
/**
* Builds a card to display information about a recent thread from this sender.
*
* #param {String} senderEmail The sender email.
* #param {Object} threadData Infomation about the thread to display.
* #return {Card} a card that displays thread information.
*/
function buildRecentThreadCard(senderEmail, threadData) {
var card = CardService.newCardBuilder();
card.setHeader(CardService.newCardHeader().setTitle(threadData.subject));
var section = CardService.newCardSection()
.setHeader("<font color=\"#1257e0\">Recent thread</font>");
section.addWidget(CardService.newTextParagraph().setText(threadData.subject));
section.addWidget(CardService.newKeyValue()
.setTopLabel('Sender')
.setContent(senderEmail));
section.addWidget(CardService.newKeyValue()
.setTopLabel('Number of messages')
.setContent(threadData.count.toString()));
section.addWidget(CardService.newKeyValue()
.setTopLabel('Last updated')
.setContent(threadData.lastDate.toString()));
var threadLink = CardService.newOpenLink()
.setUrl(threadData.link)
.setOpenAs(CardService.OpenAs.FULL_SIZE);
var button = CardService.newTextButton()
.setText('Open Thread')
.setOpenLink(threadLink);
section.addWidget(CardService.newButtonSet().addButton(button));
card.addSection(section);
return card.build();
}
function build_login_card(){
var card = CardService.newCardBuilder();
card.setHeader(CardService.newCardHeader().setTitle("Login Here"));
var userProperties = PropertiesService.getUserProperties();
var Token = userProperties.setProperty('Token',"Token");
return card.build()
}
According to comments, the primary issue here is that uninstalling the add-on does not remove bits from the UserProperties datastore, and thus if the add-on is re-installed, the add-on configuration steps cannot be performed again.
Generic Add-on Solution
If this were not a Gmail add-on, this could be simply solved with a time-based trigger, since per documentation:
Add-on triggers will stop firing in any of the following situations:
- If the add-on is uninstalled by the user
- If the add-on is disabled in a document (if it is re-enabled, the trigger will become operational again)
- If the developer unpublishes the add-on or submits a broken version to the add-on store
I.e., you would simply write the day's date to a key (e.g. LAST_SEEN) in PropertiesService#UserProperties every day, and then check both TOKEN and LAST_SEEN when deciding which card to display.
Gmail Add-on Solution:
Per Gmail add-on documentation, you cannot create or use Apps Script simple / installable triggers in a Gmail add-on. So, the easiest implementation of the solution is not available. However, we can still use this approach, by moving the region in which that LAST_SEEN key is updated.
Right now (2018 March), the only available trigger for a Gmail add-on is the contextual trigger unconditional:
Currently, the only contextual trigger type available is unconditional, which triggers for all emails regardless of content.
So, if you bind to this contextual trigger, while your add-on is installed it will run a function whenever the user opens an email. The solution is then for your contextually triggered function to include the snippet
PropertiesService.getUserProperties().setProperty("LAST_SEEN", String(new Date().getTime()));
If you have other stuff to do in your contextually triggered function, that code will be uninfluenced by this addition. If you don't have a contextually triggered function, then you'd want to return [] (an empty card stack) in order to avoid showing any UI.
To use this new property, in your buildAddon(e) method you want to query for this value in addition to the TOKEN property you are using:
var userProps = PropertiesService.getUserProperties().getAll();
var Token = userProps["Token"];
var lastSeen = userProps["LAST_SEEN"] || 0; // If found, will be milliseconds since epoch.
var absence = new Date().getTime() - lastSeen; // Time in ms since last use of add-on.
if (Token == null || absence > /* some duration you choose */ ) {
// New install, or user has started using app again.
return [build_login_card()];
} else {
// User is still using add-on, so do normal stuff.
}
This is obviously not a perfect solution (i.e. an uninstall contextual trigger would be much better), but can help detect lack-of-use situations
There are rate limits on how often you can write to PropertiesService. If you have speedy/"power" users, they might trip quotas.
Could combine CacheService and PropertiesService to handle frequent reads in a "short" session (of up to 6hr since last storage into cache).
Robert , have you tried caching the user input ?
I do caching in the event handler.
function onDomainChange(e){
var cache = CacheService.getScriptCache();
Logger.log(e.formInput);
cache.put('domain',e.formInput.domain);
Logger.log(cache.get('domain'));
}
Refer cache docs

How to Embed a Google Form in Email in Google App Script

I have a form that requires login and I am trying to fetch its html to embed in an email. Similar to the question Google Apps Script: how to access or call "Send this form to others"?
var form = FormApp.create('New Form');
form.setRequireLogin(true);
...
var url = form.getPublishedUrl();
var response = UrlFetchApp.fetch(url);
var htmlBody = HtmlService.createHtmlOutput(response).getContent();
MailApp.sendEmail({
to: email,
subject: subject,
htmlBody: htmlBody,
});
...
Howerver, the URLFetchApp does not seem to have the correct OAuth configuration and I always get the HTML for Google Login page.
Is there a way to set the correct OAuth parameters to get the form HTML?
Your question asks about the OAuth parameters required to allow UrlFetch() to obtain the HTML content of a form with requiresLogin() set. This answer doesn't address that directly.
Without requiring OAuth, you can briefly change the login requirement for the form, just long enough to grab the HTML from it. For a small amount of time, your form could be accessed by individuals outside of your domain, if they happened to have the URL, and filled the form fast enough to submit their response before you locked the form up again.
Script
The following sendForm() function will work for consumer & GApps domain accounts, whether or not requiresLogin() is set.
/**
* Send user an email containing the given form, in HTML.
*
* #param {Form} form Form object.
* #param {String} email One or more email addresses, comma separated.
*/
function sendForm(form,email) {
var url = form.getPublishedUrl();
// Temporarily disable requiresLogin so UrlFetch will operate
if (form.requiresLogin()) {
var requiresLogin = true;
form.setRequireLogin(false);
}
// Fetch form's HTML
var response = UrlFetchApp.fetch(url);
var htmlBody = HtmlService.createHtmlOutput(response).getContent();
// Re-enable requireLogin, if necessary
if (requiresLogin) {
form.setRequireLogin(true);
}
var subject = form.getTitle();
MailApp.sendEmail(email,
subject,
'This message requires HTML support to view.',
{
name: 'Form Emailer Script',
htmlBody: htmlBody
});
}
For completeness, here's a test function...
function test_sendForm() {
// Build new form for testing
var form = FormApp.create('New Form');
var formTitle = 'Form Name';
form.setTitle(formTitle)
.setDescription('Description of form')
.setConfirmationMessage('Thanks for responding!')
.setAllowResponseEdits(true)
.setAcceptingResponses(true)
// Require Login (for GApp Domain accounts only)
try {
form.setRequireLogin(true);
} catch (e) {
// Error is expected for consumer accounts - carry on.
}
// Just one question
form.addTextItem().setTitle("Q1");
// Send it to self
var email = Session.getEffectiveUser().getEmail();
sendForm(form,email)
}

(Gmail) Sending emails from spreadsheet. How to add signature with image?

Due to the large amount of emails i'm sending with GMAIL i decided to automatize this process using a script and following this tutorial.Tutorial: Sending emails from a Spreadsheet
The "Message" is being generated by another function i created, called prepareEmails.
The problems are the following:
1) How can i tell prepareEmails to add my personal signature? I can't simply copy its text into that function, because my signature contains an image (for which i have the URL), and i want that image to be into the signature.
2) How can i make my signature BOLD?
Thanks everybody
There is an open Issue 2441 requesting the ability to append gmail signatures to email when using the GMailService. Visit and star it to receive updates.
As #wchiquito suggests, you can craft a script to attach images, producing a signature. You can also use HTML tags such as <B></B> to render text in bold, and so on.
Here's a different approach that will instead use a draft email as a template. This way, you can produce your signature with a variety of fonts and images using the online editor, and end up with a capability similar to automatic signature insertion.
The template needs to be saved in your Drafts folder, and it needs to have a tag indicating where the body of emails should go.
Example
function sendWithTemplate() {
var msgBody = "Test of sending a message using a template with a signature.";
sendGmailTemplate(Session.getActiveUser().getEmail(), 'test', msgBody );
}
Script
/**
* Insert the given email body text into an email template, and send
* it to the indicated recipient. The template is a draft message with
* the subject "TEMPLATE"; if the template message is not found, an
* exception will be thrown. The template must contain text indicating
* where email content should be placed: {BODY}.
*
* #param {String} recipient Email address to send message to.
* #param {String} subject Subject line for email.
* #param {String} body Email content, may be plain text or HTML.
* #param {Object} options (optional) Options as supported by GmailApp.
*
* #returns GmailApp the Gmail service, useful for chaining
*/
function sendGmailTemplate(recipient, subject, body, options) {
options = options || {}; // default is no options
var drafts = GmailApp.getDraftMessages();
var found = false;
for (var i=0; i<drafts.length && !found; i++) {
if (drafts[i].getSubject() == "TEMPLATE") {
found = true;
var template = drafts[i];
}
}
if (!found) throw new Error( "TEMPLATE not found in drafts folder" );
// Generate htmlBody from template, with provided text body
var imgUpdates = updateInlineImages(template);
options.htmlBody = imgUpdates.templateBody.replace('{BODY}', body);
options.attachments = imgUpdates.attachments;
options.inlineImages = imgUpdates.inlineImages;
return GmailApp.sendEmail(recipient, subject, body, options);
}
/**
* This function was adapted from YetAnotherMailMerge by Romain Vaillard.
* Given a template email message, identify any attachments that are used
* as inline images in the message, and move them from the attachments list
* to the inlineImages list, updating the body of the message accordingly.
*
* #param {GmailMessage} template Message to use as template
* #returns {Object} An object containing the updated
* templateBody, attachments and inlineImages.
*/
function updateInlineImages(template) {
//////////////////////////////////////////////////////////////////////////////
// Get inline images and make sure they stay as inline images
//////////////////////////////////////////////////////////////////////////////
var templateBody = template.getBody();
var rawContent = template.getRawContent();
var attachments = template.getAttachments();
var regMessageId = new RegExp(template.getId(), "g");
if (templateBody.match(regMessageId) != null) {
var inlineImages = {};
var nbrOfImg = templateBody.match(regMessageId).length;
var imgVars = templateBody.match(/<img[^>]+>/g);
var imgToReplace = [];
if(imgVars != null){
for (var i = 0; i < imgVars.length; i++) {
if (imgVars[i].search(regMessageId) != -1) {
var id = imgVars[i].match(/realattid=([^&]+)&/);
if (id != null) {
var temp = rawContent.split(id[1])[1];
temp = temp.substr(temp.lastIndexOf('Content-Type'));
var imgTitle = temp.match(/name="([^"]+)"/);
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(/src="[^\"]+\"/, "src=\"cid:" + imgToReplace[i][2] + "\"");
templateBody = templateBody.replace(imgToReplace[i][1], newImg);
}
}
}
}
var updatedTemplate = {
templateBody: templateBody,
attachments: attachments,
inlineImages: inlineImages
}
return updatedTemplate;
}
Credit where credit is due: The "Yet Another Mail Merge" script includes code that preserves inline images in emails during a mail merge - I've borrowed from that. Thanks Romain!
Currently the API does not offer anything to include the signature to messages, however, if you have control of the signature, you can use the method sendEmail(recipient, subject, body, options) available in classes GmailApp/MailApp. The options parameter allows you to set additional parameters to get what you need, for example, include images, with which the signature can build manually, and sets text in bold.
I invite you to take a look at the documentation/examples and post any questions you may have. An interesting example can be found here.