【发布时间】:2017-05-09 03:46:17
【问题描述】:
有没有办法使用 Google 电子表格中的 Javascript(或其他)来获取 Gmail 帐户签名?
详情: 我有一个包含信息的 Google 电子表格。 单击按钮时,它会确定谁必须接收信息、准备邮件并将其发送给该人。 但是,我想在邮件末尾添加发件人的签名(包括姓名、电话、徽标等)。 如果我从其他地方获得签名,我是开放的,只要它可以根据发送邮件的人而改变。 这是给我的志愿者协会的,而不是一份工作。
如果我的代码可能有帮助(我希望填写 var 签名):
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);
解决方案:
我从另一个网站找到了一条路(找不到): 在您的 gmail 草稿中使用您的签名创建一个模板,并在主题“模板”和正文 {Body} 中输入。 然后使用以下代码创建邮件的副本,并填写所有信息:
在发送邮件的函数中添加:
sendGmailTemplate(RecipientTo, RecipientCC, RecipientBCC, SujetAEnvoyer, CourrielAEnvoyer);
参考以下函数:
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;
}
【问题讨论】:
-
如果您为自己提供了一个解决方案,您可以创建一个答案并接受它,因此该主题可以显示为已回答。
标签: javascript google-apps-script google-sheets gmail