Is there a way with Javascript (or other) from Google Spreadsheet to get the Gmail account signature?
Details: I have a Google spreadsheet with information in it. When clicking on a button, it identifies who has to receive the information, prepare a mail, and send it to the person. However, I want to add the sender's signature at the end of the mail (it includes name, phone, logo, etc.). I'm open if I get the signature from an other place, as long as it can change according to who's sending the mail. This is for my volunteering association, not a job.
In case my code may help (I look to fill the var Signature):
var Signature;
var Receivers;
var Subject;
var Location ;
var MailtoSend= "Hello, \n\n The next meeting will be at " + Location + ". \n" + Signature;
MailApp.sendEmail(Receivers, Subject, MailtoSend);
SOLUTION:
I've find a way from another site (can't find it): Create a template in your gmail draft with your signature, and put in the subject "Template" and in the body {Body}. Then use the following code to create a copy of the mail, and fill it with all the information:
In the function to send the mail add:
sendGmailTemplate(RecipientTo, RecipientCC, RecipientBCC, SujetAEnvoyer, CourrielAEnvoyer);
Referring to the following functions:
function sendGmailTemplate(RecipientTo, RecipientCC, RecipientBCC, subject, body, options) { //mettre à jour la quantité de recipeien cc bcc to
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( "Impossible de trouver le brouillon 'Template' sur le gmail" );
// 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;
options.cc = RecipientCC;
options.bcc = RecipientBCC;
options.replyTo = "";
return GmailApp.sendEmail(RecipientTo, subject, body, options);
}
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;
}