【问题标题】:Javascript Send mail on Gmail with signature and BCC with Google SpreadsheetJavascript 在 Gmail 上发送带有签名的邮件,并使用 Google 电子表格发送密件抄送
【发布时间】: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


【解决方案1】:

没有一种方法可以请求您已经创建的签名。可以使用 inlineImages 自己创建一个。可以在此处找到相关文档:https://developers.google.com/apps-script/reference/gmail/gmail-app#sendemailrecipient-subject-body-options

【讨论】:

  • 嗨,乔丹,感谢您提供的信息。如果我不能绕过我的需要,我会考虑作为 B 计划:)
猜你喜欢
  • 2012-03-20
  • 2013-06-08
  • 1970-01-01
  • 2016-03-04
  • 1970-01-01
  • 2018-02-05
  • 2015-08-08
  • 2020-10-10
相关资源
最近更新 更多